CREATE TABLE edna_channels (
    id NUMBER(19) PRIMARY KEY REFERENCES channels(id),
    uuid VARCHAR2(255 CHAR),
    phone VARCHAR2(255 CHAR),
    deleted NUMBER(1),

    created_at TIMESTAMP WITH TIME ZONE NOT NULL,
    updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
    deleted_at TIMESTAMP WITH TIME ZONE,
    restored_at TIMESTAMP WITH TIME ZONE
);

CREATE UNIQUE INDEX edna_channels_uuid_ix ON edna_channels(uuid);

CREATE TABLE edna_configs (
    channel_id NUMBER(19) PRIMARY KEY REFERENCES edna_channels(id),

    url VARCHAR2(255 CHAR),
    phone VARCHAR2(255 CHAR),
    api_key VARCHAR2(255 CHAR),
    message_subject VARCHAR2(255 CHAR)
);

CREATE TABLE edna_users (
    id NUMBER(19) PRIMARY KEY,
    phone VARCHAR2(255 CHAR) NOT NULL,
    name VARCHAR2(255 CHAR),

    created_at TIMESTAMP WITH TIME ZONE NOT NULL,
    updated_at TIMESTAMP WITH TIME ZONE NOT NULL
);

CREATE SEQUENCE edna_users_id_seq;

CREATE UNIQUE INDEX edna_users_phone_ix ON edna_users(phone);

CREATE TABLE edna_chats (
    id NUMBER(19) PRIMARY KEY,
    chat_id NUMBER(19) NOT NULL REFERENCES chats(id),
    user_id NUMBER(19) NOT NULL REFERENCES edna_users(id),

    created_at TIMESTAMP WITH TIME ZONE NOT NULL,
    updated_at TIMESTAMP WITH TIME ZONE NOT NULL
);

CREATE SEQUENCE edna_chats_id_seq;