create table user_fcm
(
    enabled    BIT            not null,
    settings   varchar(4000)  not null,
    created_at DATETIMEOFFSET not null DEFAULT sysdatetimeoffset(),
    updated_at DATETIMEOFFSET not null
);

CREATE TABLE user_push_tokens
(
    id         BIGINT IDENTITY (1, 1) PRIMARY KEY,
    uid        VARCHAR(40)    NOT NULL,
    user_id    BIGINT         NOT NULL REFERENCES users (id),
    type       VARCHAR(16)    NOT NULL,
    token      VARCHAR(4000)  NOT NULL,
    active     BIT            NOT NULL DEFAULT 0,
    created_at DATETIMEOFFSET NOT NULL DEFAULT sysdatetimeoffset(),
    updated_at DATETIMEOFFSET NOT NULL DEFAULT sysdatetimeoffset()
);

CREATE TABLE user_pushes
(
    id           BIGINT IDENTITY (1, 1) PRIMARY KEY,
    state        VARCHAR(16)    NOT NULL,
    type         varchar(16)    not null,
    token_id     BIGINT         NOT NULL REFERENCES user_push_tokens (id),
    message_id   BIGINT REFERENCES ticket_messages (id),
    created_at   DATETIMEOFFSET NOT NULL DEFAULT sysdatetimeoffset(),
    sent_at      DATETIMEOFFSET,
    cancelled_at DATETIMEOFFSET
);


CREATE UNIQUE INDEX user_push_tokens_uid_uniq
    ON user_push_tokens (uid);
CREATE UNIQUE INDEX user_push_tokens_client_id_uid_uniq
    ON user_push_tokens (user_id, uid);
