-- Benachrichtigungs-Dispatcher-Warteschlange (CFG-02, siehe
-- core-kanban/tickets/CFG-02.md). Postgres-basiert statt Redis/AMQP
-- (Projekt-Konvention, siehe nexarch-state.json techstack.job_queue) —
-- Zeilen ueberleben einen Neustart des Dispatcher-Prozesses unveraendert
-- (Akzeptanzkriterium 3).
CREATE TABLE notification_jobs (
    id              UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    channel         TEXT NOT NULL,
    recipient       TEXT NOT NULL,
    payload         JSONB NOT NULL DEFAULT '{}'::jsonb,
    status          TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'sent', 'failed')),
    attempts        INT NOT NULL DEFAULT 0,
    max_attempts    INT NOT NULL DEFAULT 5,
    next_attempt_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    last_error      TEXT,
    created_at      TIMESTAMPTZ NOT NULL DEFAULT now(),
    updated_at      TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX notification_jobs_due_idx ON notification_jobs (status, next_attempt_at);
