-- Part 6: outbox shape
-- Standalone extension table. Use alongside the shared claim-loop style.

CREATE TABLE outbox (
  id uuid PRIMARY KEY DEFAULT uuidv7(),
  partition_key text NOT NULL,
  event_type text NOT NULL,
  payload jsonb NOT NULL,
  status text NOT NULL DEFAULT 'pending',
  idempotency_key text,
  available_at timestamptz NOT NULL DEFAULT now(),
  created_at timestamptz NOT NULL DEFAULT now()
);

CREATE UNIQUE INDEX idx_outbox_idempotency
  ON outbox (idempotency_key)
  WHERE idempotency_key IS NOT NULL;

INSERT INTO outbox (partition_key, event_type, payload, idempotency_key)
VALUES (
  'order:9182',
  'OrderPaid',
  '{"order_id":9182,"paid_at":"2026-07-01T12:00:00Z"}'::jsonb,
  'evt-order-9182-paid-v1'
);
