-- Per-request error capture table powering the super-admin error
-- inspector. A user pastes the "Error ID: …" they saw on a failed
-- mutation; the admin pulls the full row.
--
-- Pruned at 90 days by the maintenance worker.

CREATE TABLE IF NOT EXISTS error_events (
  request_id text PRIMARY KEY,
  port_id text REFERENCES ports(id) ON DELETE SET NULL,
  user_id text,
  status_code integer NOT NULL,
  method text NOT NULL,
  path text NOT NULL,
  error_name text,
  error_message text,
  error_stack text,
  request_body_excerpt text,
  user_agent text,
  ip_address text,
  duration_ms integer,
  metadata jsonb DEFAULT '{}'::jsonb,
  created_at timestamptz NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_error_events_port_created
  ON error_events(port_id, created_at);
CREATE INDEX IF NOT EXISTS idx_error_events_status_created
  ON error_events(status_code, created_at);
