-- 030_realtime_events.sql —— SSE 事件持久化与断线补发。

CREATE TABLE IF NOT EXISTS realtime_events (
  id BIGSERIAL PRIMARY KEY,
  event_type TEXT NOT NULL,
  entity_id BIGINT NOT NULL DEFAULT 0,
  scopes JSONB NOT NULL DEFAULT '[]'::jsonb,
  operation TEXT NOT NULL DEFAULT 'refresh',
  data JSONB,
  admin_audience BOOLEAN NOT NULL DEFAULT FALSE,
  worker_ids JSONB NOT NULL DEFAULT '[]'::jsonb,
  broadcast_workers BOOLEAN NOT NULL DEFAULT FALSE,
  occurred_at TIMESTAMPTZ NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_realtime_events_occurred_at
  ON realtime_events(occurred_at DESC);

COMMENT ON TABLE realtime_events IS 'SSE 实时事件日志，保留最近事件以支持断线后的增量补发';
