-- adr091: aider_events schema
-- 2026-04-20 @ Asia/Taipei
-- 紀錄統帥本機 aider CLI 活動，供 AI Router feedback + symptom_pattern 抽取

CREATE TABLE IF NOT EXISTS aider_events (
  id              BIGSERIAL PRIMARY KEY,
  session_id      TEXT NOT NULL,
  ts              TIMESTAMPTZ NOT NULL,
  type            TEXT NOT NULL,                  -- session_start|file_edit|error|commit|silent_timeout|session_end|raw
  host            TEXT DEFAULT 'ogt-mac',
  payload         JSONB NOT NULL,
  incident_id     TEXT,
  created_at      TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS aider_events_session_idx ON aider_events(session_id);
CREATE INDEX IF NOT EXISTS aider_events_type_ts_idx ON aider_events(type, ts DESC);
CREATE INDEX IF NOT EXISTS aider_events_ts_idx ON aider_events(ts DESC);
CREATE INDEX IF NOT EXISTS aider_events_payload_gin ON aider_events USING GIN (payload);

COMMENT ON TABLE aider_events IS 'aider CLI 事件流（Mac 端 aiderw wrapper 推入）';
COMMENT ON COLUMN aider_events.incident_id IS '若觸發建 incident，記 FK 至 incidents.incident_id';
COMMENT ON COLUMN aider_events.payload IS 'Type-specific payload JSON，見 src/models/aider.py schema';
