--- mailings

create table mailings (
    id bigint primary key,
    project_id bigint not null references projects(id),
    state varchar(255) not null,

    title varchar(255) not null,
    user_id bigint references users(id),
    channel_ids varchar(4000) not null,

    created_at timestamptz not null,
    updated_at timestamptz not null,
    deleted_at timestamptz
);

create sequence mailings_id_seq;

create index mailings_project_ix 
on mailings(project_id);

-- messages

create table mailing_messages (
    id bigint primary key,
    mailing_id bigint not null references mailings(id),
    
    user_id bigint not null references users(id),
    sender_id bigint not null references users(id),
    state varchar(255) not null,

    "type" varchar(255) not null,
    "text" varchar(2000) not null,
    file_id varchar(255) references files(id),

    created_at timestamptz not null,
    updated_at timestamptz not null,
    sent_at timestamptz
);

create sequence mailing_messages_id_seq;

create index mailing_messages_mailing_ix on mailing_messages(mailing_id, created_at desc);
