ALTER TABLE gateway.outbox_events
    ADD COLUMN IF NOT EXISTS dead_lettered_at timestamptz,
    ADD COLUMN IF NOT EXISTS published_stream_id text;

CREATE INDEX IF NOT EXISTS outbox_events_claimable_idx
    ON gateway.outbox_events (available_at, occurred_at)
    WHERE processed_at IS NULL AND dead_lettered_at IS NULL;

CREATE INDEX IF NOT EXISTS outbox_events_dead_letter_idx
    ON gateway.outbox_events (dead_lettered_at DESC)
    WHERE dead_lettered_at IS NOT NULL;

COMMENT ON COLUMN gateway.outbox_events.published_stream_id IS
    'Redis Stream entry ID, or duplicate when the event marker proved it was already published.';
