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

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

CREATE UNIQUE INDEX edna_chatbot_channels_uuid_ix ON edna_chatbot_channels(uuid);

CREATE TABLE edna_chatbot_configs (
                              channel_id BIGINT PRIMARY KEY REFERENCES edna_chatbot_channels(id),

                              url VARCHAR(255),
                              api_key VARCHAR(255)
);

CREATE TABLE edna_chatbot_users (
                            id bigint PRIMARY KEY not null ,
                            name VARCHAR(255),
                            phone varchar(15),
                            email varchar(225),
                            crm_id varchar(255),
                            edna_client_id varchar(255),
                            created_at TIMESTAMPTZ NOT NULL,
                            updated_at TIMESTAMPTZ NOT NULL
);

CREATE SEQUENCE IF NOT EXISTS edna_chatbot_users_id_seq;

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

                            created_at TIMESTAMPTZ NOT NULL,
                            updated_at TIMESTAMPTZ NOT NULL
);

alter table clients add edna_chatbot_id bigint;
alter table tickets add edna_session_id varchar(255);