-- Error log: captures runtime exceptions from CMS endpoints and workers
-- Viewed in Ops → Health module

CREATE TABLE IF NOT EXISTS error_log (
  id           BIGSERIAL PRIMARY KEY,
  source       TEXT NOT NULL,
  severity     TEXT NOT NULL DEFAULT 'error',
  message      TEXT NOT NULL,
  stack        TEXT,
  context      JSONB,
  actor_email  TEXT,
  request_path TEXT,
  created_at   TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_error_log_created ON error_log (created_at DESC);
CREATE INDEX IF NOT EXISTS idx_error_log_source ON error_log (source, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_error_log_severity ON error_log (severity, created_at DESC);
