create table user_fcm
(
    enabled    NUMBER(1) not null,
    settings   varchar2(4000 CHAR) not null,
    created_at TIMESTAMP WITH TIME ZONE not null,
    updated_at TIMESTAMP WITH TIME ZONE not null
);

CREATE TABLE user_pushes
(
    id           NUMBER(19) PRIMARY KEY,
    token_id     NUMBER(19) NOT NULL REFERENCES user_push_tokens (id),
    message_id   NUMBER(19) REFERENCES ticket_messages (id),
    state        VARCHAR2(16 CHAR) NOT NULL,
    type         varchar2(16 char) not null,
    created_at   TIMESTAMP WITH TIME ZONE NOT NULL,
    sent_at      TIMESTAMP WITH TIME ZONE,
    cancelled_at TIMESTAMP WITH TIME ZONE
);


CREATE TABLE user_push_tokens
(
    id         NUMBER(19) PRIMARY KEY,
    "uid"      VARCHAR2(40 CHAR) NOT NULL,
    user_id    NUMBER(19) NOT NULL REFERENCES users (id),
    type       VARCHAR2(16 CHAR) NOT NULL,
    token      VARCHAR2(4000 CHAR) NOT NULL,
    active     NUMBER(1) NOT NULL default 0,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL,
    updated_at TIMESTAMP WITH TIME ZONE NOT NULL
);

CREATE SEQUENCE user_push_tokens_id_seq;

CREATE UNIQUE INDEX user_push_tokens_uid_uniq
    ON push_tokens ("uid");

CREATE INDEX user_push_tokens_client_id
    ON user_push_tokens (user_id, "uid");

CREATE SEQUENCE user_pushes_id_seq;

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