-- Weekly report e-mail: operator-confirmed drafts are sent at the configured slot. -- The SMTP password is sealed with the independently provisioned master key, never stored in clear. create table if not exists qa_weekly_email_settings ( tenant_key text primary key, smtp_host text not null default '', smtp_port integer not null default 465 check (smtp_port between 1 and 65535), smtp_username text not null default '', smtp_password_sealed bytea, from_address text not null default '', recipients text[] not null default '{}', send_weekday smallint not null default 1 check (send_weekday between 1 and 7), send_hour smallint not null default 9 check (send_hour between 0 and 23), enabled boolean not null default false, revision bigint not null default 0, updated_by text not null default '', updated_at timestamptz not null default now() ); create table if not exists qa_weekly_confirmations ( tenant_key text not null, week text not null, draft_revision bigint not null check (draft_revision > 0), body_sha256 text not null, confirmed_by text not null, confirmed_at timestamptz not null default now(), primary key (tenant_key, week) ); create table if not exists qa_weekly_email_sends ( id uuid primary key default gen_random_uuid(), tenant_key text not null, week text not null, draft_revision bigint not null, body_sha256 text not null, status text not null check (status in ('sending', 'sent', 'failed')), attempts integer not null default 0, last_error text not null default '', message_id text not null default '', created_at timestamptz not null default now(), updated_at timestamptz not null default now(), unique (tenant_key, week, draft_revision) ); create index if not exists qa_weekly_email_sends_week_idx on qa_weekly_email_sends(tenant_key, week);