CREATE TABLE vk_publics (
                                  id BIGINT IDENTITY (1, 1) primary key ,
                                  title NVARCHAR(125) not null,
                                  vk_link NVARCHAR(512) not null,
                                  project_id BIGINT NOT NULL references projects(id),
                                  vk_group_id BIGINT not null,
                                  channel_id BIGINT NOT NULL references channels(id),
                                  user_token nvarchar(2000),
                                    [deleted] BIT NOT NULL,

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

CREATE UNIQUE INDEX vk_publics_uniq ON vk_publics(vk_link, channel_id) WHERE deleted = 0;

create table vk_posts (
                                id bigint IDENTITY (1, 1) primary key,
                                public_id bigint not null references vk_publics (id),
                                vk_id bigint,
                                poll_id bigint,
                                poll_end_date bigint,
                                user_id bigint not null references users (id),
                                state NVARCHAR(255) not null,

                                [type] NVARCHAR(255) not null,
                                [text] NVARCHAR(2000) not null,
                                header nvarchar(500) not null,
                                background_id integer,
                                background_image_id VARCHAR(255) references files(id),
                                is_multiple bit,
                                has_files bit not null,
                                deleted bit not null,

                                created_at DATETIMEOFFSET NOT NULL,
                                updated_at DATETIMEOFFSET NOT NULL,
                                sent_at DATETIMEOFFSET,
                                deleted_at DATETIMEOFFSET
);
create index vk_posts_public_ix on vk_posts(public_id, created_at desc) where deleted = 0;
