create table user_fcm
(
    enabled    boolean       not null,
    settings   varchar(4000) not null,
    created_at timestamptz   not null DEFAULT now(),
    updated_at timestamptz   not null
);

CREATE TABLE user_push_tokens
(
    id         BIGINT PRIMARY KEY,
    uid        VARCHAR(40) NOT NULL,
    user_id    BIGINT      NOT NULL REFERENCES users (id),
    type       VARCHAR(16) NOT NULL,
    token      VARCHAR,
    active     BOOLEAN     NOT NULL DEFAULT FALSE,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE TABLE user_pushes
(
    id           BIGINT 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   TIMESTAMPTZ NOT NULL DEFAULT now(),
    sent_at      TIMESTAMPTZ,
    cancelled_at TIMESTAMPTZ
);
CREATE SEQUENCE user_pushes_id_seq;

CREATE UNIQUE INDEX user_pushes_token_message_uniq
    ON user_pushes (token_id, message_id);

CREATE SEQUENCE user_push_tokens_id_seq;
CREATE UNIQUE INDEX user_push_tokens_uid_uniq
    ON user_push_tokens (uid);
CREATE UNIQUE INDEX user_push_tokens_user_id_uid_uniq
    ON user_push_tokens (user_id, uid);
