--- infobip

create table infobip_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 infobip_channels_uuid_ix on infobip_channels(uuid);

-- configs

create table infobip_configs (
    channel_id bigint primary key references infobip_channels(id),

    url nvarchar(255),
    phone nvarchar(255),
    username nvarchar(255),
    password nvarchar(255),

    scenario_key nvarchar(255)
);

-- users

create table infobip_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 infobip_users_phone_ix on infobip_users(phone);

-- chats

create table infobip_chats (
    id bigint primary key identity (1, 1),
    chat_id bigint not null references chats(id),
    user_id bigint not null references infobip_users(id),

    created_at datetimeoffset not null,
    updated_at datetimeoffset not null
);
