CREATE TABLE vk_publics (
                                  id BIGINT PRIMARY KEY,
                                  title varchar(125) not null,
                                  vk_link varchar(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 varchar,
                                  deleted BOOLEAN NOT NULL,

                                  created_at TIMESTAMP WITH TIME ZONE NOT NULL,
                                  updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
                                  deleted_at TIMESTAMP WITH TIME ZONE
);

CREATE SEQUENCE vk_publics_id_seq;

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

create table vk_posts (
                                id bigint 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 varchar(255) not null,

                                "type" varchar(255) not null,
                                "text" varchar(2000) not null,
                                header varchar(500) not null,
                                background_id integer,
                                background_image_id varchar(255) references files(id),
                                is_multiple boolean,
                                has_files boolean not null,
                                deleted boolean not null,
                                created_at timestamptz not null,
                                updated_at timestamptz not null,
                                sent_at timestamptz,
                                deleted_at timestamptz
);

create sequence vk_posts_id_seq;

create index vk_posts_public_ix on vk_posts(public_id, created_at desc);
