CREATE TABLE edna_channels (
    id BIGINT PRIMARY KEY REFERENCES channels(id),
    uuid NVARCHAR(255),
    phone NVARCHAR(255),
    deleted BIT,

    created_at DATETIMEOFFSET NOT NULL,
    updated_at DATETIMEOFFSET NOT NULL,
    deleted_at DATETIMEOFFSET,
    restored_at DATETIMEOFFSET
);

CREATE UNIQUE INDEX edna_channels_uuid_ix ON edna_channels(uuid);

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

    url NVARCHAR(255),
    phone NVARCHAR(255),
    api_key NVARCHAR(255),
    message_subject NVARCHAR(255)
);

CREATE TABLE edna_users (
    id BIGINT PRIMARY KEY IDENTITY (1, 1),
    phone NVARCHAR(255) NOT NULL,
    name NVARCHAR(255),

    created_at DATETIMEOFFSET NOT NULL,
    updated_at DATETIMEOFFSET NOT NULL
);

CREATE UNIQUE INDEX edna_users_phone_ix ON edna_users(phone);

CREATE TABLE edna_chats (
    id BIGINT PRIMARY KEY IDENTITY (1, 1),
    chat_id BIGINT NOT NULL REFERENCES chats(id),
    user_id BIGINT NOT NULL REFERENCES edna_users(id),

    created_at DATETIMEOFFSET NOT NULL,
    updated_at DATETIMEOFFSET NOT NULL
);
