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

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

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 VARCHAR(255),
    phone VARCHAR(255),
    api_key VARCHAR(255),
    message_subject VARCHAR(255)
);

CREATE TABLE edna_users (
    id BIGSERIAL PRIMARY KEY,
    phone VARCHAR(255) NOT NULL,
    name VARCHAR(255),

    created_at TIMESTAMPTZ NOT NULL,
    updated_at TIMESTAMPTZ NOT NULL
);

CREATE UNIQUE INDEX edna_users_phone_ix ON edna_users(phone);

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

    created_at TIMESTAMPTZ NOT NULL,
    updated_at TIMESTAMPTZ NOT NULL
);
