--- mailings

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

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

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

create index mailings_project_ix
on mailings(project_id);

-- messages

create table mailing_messages (
    id bigint primary key identity (1, 1),
    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 nvarchar(255) not null,

    [type] nvarchar(255) not null,
    [text] nvarchar(2000) not null,
    file_id varchar(255) references files(id),

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

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