-- viber-channels

CREATE TABLE viber_channels
(
    id bigint primary key,
    uuid NVARCHAR(36),
    deleted BIT NOT NULL,
    user_name NVARCHAR(1024),
    user_id NVARCHAR(256),
    icon NVARCHAR(512),
    uri NVARCHAR(1024),
    created_at datetimeoffset NOT NULL,
    updated_at datetimeoffset NOT NULL,
    deleted_at datetimeoffset,
    restored_at datetimeoffset
);

CREATE UNIQUE INDEX viber_channels_uuid_uniq
    ON viber_channels(uuid);

CREATE UNIQUE INDEX viber_channels_user_uniq
    ON viber_channels (user_id);

-- viber-chats

CREATE TABLE viber_chats
(
    id bigint PRIMARY KEY IDENTITY(1,1),
    chat_id bigint NOT NULL REFERENCES chats(id),
    created_at datetimeoffset NOT NULL,
    updated_at datetimeoffset NOT NULL
);

CREATE UNIQUE INDEX viber_chats_chat_uniq
    ON viber_chats(chat_id);

-- viber_congigs

CREATE TABLE viber_configs
(
    channel_id bigint PRIMARY KEY NOT NULL REFERENCES channels(id),
    bot_token NVARCHAR(255)
);

-- viber users

CREATE TABLE viber_users
(
    id bigint PRIMARY KEY IDENTITY(1,1),
    viber_id NVARCHAR(256),
    name NVARCHAR(512),
    avatar NVARCHAR(4000),
    created_at datetimeoffset NOT NULL,
    updated_at datetimeoffset NOT NULL
);

CREATE UNIQUE INDEX viber_users_id_viber_id_uniq ON viber_users(id, viber_id);

