CREATE TABLE IF NOT EXISTS council_runs (
  run_id TEXT PRIMARY KEY,
  mode TEXT NOT NULL,
  target_type TEXT NOT NULL,
  output_mode TEXT NOT NULL,
  only_unanimous INTEGER NOT NULL DEFAULT 0,
  created_at TEXT NOT NULL,
  updated_at TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS council_reviewers (
  run_id TEXT NOT NULL,
  reviewer_role TEXT NOT NULL,
  task_id TEXT NOT NULL,
  status TEXT NOT NULL,
  PRIMARY KEY (run_id, reviewer_role)
);

CREATE TABLE IF NOT EXISTS council_findings (
  run_id TEXT NOT NULL,
  reviewer_role TEXT NOT NULL,
  finding_id TEXT NOT NULL,
  title TEXT NOT NULL,
  summary TEXT NOT NULL,
  proposal TEXT NOT NULL,
  rationale TEXT NOT NULL,
  confidence TEXT NOT NULL,
  tags_json TEXT NOT NULL DEFAULT '[]',
  target_refs_json TEXT NOT NULL DEFAULT '{}',
  PRIMARY KEY (run_id, reviewer_role, finding_id)
);

CREATE TABLE IF NOT EXISTS council_groups (
  run_id TEXT NOT NULL,
  group_id TEXT NOT NULL,
  proposal TEXT NOT NULL,
  bucket TEXT NOT NULL,
  support_count INTEGER NOT NULL,
  supporters_json TEXT NOT NULL DEFAULT '[]',
  dissenters_json TEXT NOT NULL DEFAULT '[]',
  rationale_summary TEXT NOT NULL DEFAULT '',
  tags_json TEXT NOT NULL DEFAULT '[]',
  source_finding_ids_json TEXT NOT NULL DEFAULT '[]',
  PRIMARY KEY (run_id, group_id)
);
