-- Block D — real notifications: bounce / complaint tracking + suppression list.
-- The `delivered_at` column already exists from 0001.

CREATE EXTENSION IF NOT EXISTS "citext";

ALTER TABLE notifications
    ADD COLUMN IF NOT EXISTS provider_message_id TEXT,
    ADD COLUMN IF NOT EXISTS bounce_type TEXT,            -- 'permanent' | 'transient' | NULL
    ADD COLUMN IF NOT EXISTS complained BOOLEAN NOT NULL DEFAULT FALSE;

CREATE INDEX IF NOT EXISTS idx_notifications_provider_message_id
    ON notifications(provider_message_id)
    WHERE provider_message_id IS NOT NULL;

-- Suppression list: any email present here gets a silent no-op on send.
-- Populated by bounce / complaint webhooks and by guest-initiated
-- unsubscribe clicks.
CREATE TABLE IF NOT EXISTS unsubscribes (
    email      CITEXT PRIMARY KEY,
    reason     TEXT,
    source     TEXT NOT NULL DEFAULT 'manual',  -- 'bounce' | 'complaint' | 'manual' | 'user'
    created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
