-- =====================================================================
-- NOTIFICATION SCHEMA — Part 19: marketing campaigns
-- A campaign sends an SMS or Email to a user segment (resolved from identity.users)
-- via the configured channel providers.
-- =====================================================================

SET search_path TO notification, public;

CREATE TABLE IF NOT EXISTS campaigns (
    id            UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    tenant_id     UUID NOT NULL,
    name          TEXT NOT NULL,
    channel       TEXT NOT NULL,                 -- 'sms' | 'email'
    audience      TEXT NOT NULL DEFAULT 'all',   -- 'all' | 'verified' | 'with_plan'
    subject       TEXT,                          -- email only
    body_html     TEXT,                          -- email body / sms text
    template_code TEXT,                          -- optional email template
    status        TEXT NOT NULL DEFAULT 'Draft', -- Draft | Sending | Sent | Failed
    total_count   INT  NOT NULL DEFAULT 0,
    sent_count    INT  NOT NULL DEFAULT 0,
    failed_count  INT  NOT NULL DEFAULT 0,
    created_at    TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    sent_at       TIMESTAMPTZ
);

CREATE INDEX IF NOT EXISTS idx_campaigns_tenant ON campaigns (tenant_id, created_at DESC);
