-- maestro · SQLite schema v0.1（设计见 DESIGN.md §6/§9）
PRAGMA journal_mode = WAL;
PRAGMA foreign_keys = ON;

CREATE TABLE IF NOT EXISTS projects (
  id             TEXT PRIMARY KEY,
  name           TEXT NOT NULL,
  repo_path      TEXT NOT NULL UNIQUE,
  default_branch TEXT NOT NULL DEFAULT 'main',
  verify_cmd     TEXT,
  autonomy       TEXT NOT NULL DEFAULT 'manual',     -- manual | auto-easy | auto-approved
  model          TEXT,
  concurrency    INTEGER NOT NULL DEFAULT 1,
  status         TEXT NOT NULL DEFAULT 'active',      -- active | paused
  created_at     TEXT NOT NULL,
  last_sync_at   TEXT                                  -- 最近一次 todo.json 同步时间
);

CREATE TABLE IF NOT EXISTS tasks (
  id          TEXT PRIMARY KEY,
  project_id  TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
  parent_id   TEXT REFERENCES tasks(id) ON DELETE CASCADE,
  depth       INTEGER NOT NULL DEFAULT 1,            -- 1..4
  title       TEXT NOT NULL,
  complexity  TEXT NOT NULL,                          -- hard | medium | easy
  status      TEXT NOT NULL DEFAULT 'init',
  priority    INTEGER NOT NULL DEFAULT 1,            -- P0 最高 / P1 中 / P2 最低
  deps        TEXT NOT NULL DEFAULT '[]',             -- JSON array of task ids
  plan        TEXT,                                   -- Hard：分析 + 拆解
  spec        TEXT,                                   -- Medium：改动 + 理由
  operations  TEXT,                                   -- Easy：将执行的操作
  result      TEXT,                                   -- JSON TaskResult
  assignee    TEXT,                                   -- agent | human
  created_at  TEXT NOT NULL,
  updated_at  TEXT NOT NULL,
  source_ref  TEXT                                    -- 旧 todo 来源标识（todo:17 / todo:17/1A），项目内唯一
);
CREATE INDEX IF NOT EXISTS idx_tasks_project ON tasks(project_id, status);
CREATE INDEX IF NOT EXISTS idx_tasks_parent  ON tasks(parent_id);
CREATE INDEX IF NOT EXISTS idx_tasks_source  ON tasks(project_id, source_ref);

CREATE TABLE IF NOT EXISTS approvals (
  id      TEXT PRIMARY KEY,
  task_id TEXT NOT NULL REFERENCES tasks(id) ON DELETE CASCADE,
  gate    TEXT NOT NULL,                              -- plan | spec | exec
  action  TEXT NOT NULL,                              -- accept | reject
  actor   TEXT NOT NULL,
  reason  TEXT,                                       -- reject 必填
  at      TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_approvals_task ON approvals(task_id, at);

CREATE TABLE IF NOT EXISTS runs (
  id                TEXT PRIMARY KEY,
  task_id           TEXT NOT NULL REFERENCES tasks(id) ON DELETE CASCADE,
  kind              TEXT NOT NULL,                    -- planner | executor
  worktree          TEXT,
  branch            TEXT,
  status            TEXT NOT NULL,                    -- started | succeeded | failed | cancelled
  started_at        TEXT NOT NULL,
  ended_at          TEXT,
  transcript_ref    TEXT,
  claude_session_id TEXT,
  error             TEXT
);
CREATE INDEX IF NOT EXISTS idx_runs_task ON runs(task_id, started_at);

-- 追加型事件日志：审计 + 实时看板推送源
CREATE TABLE IF NOT EXISTS events (
  id         TEXT PRIMARY KEY,
  project_id TEXT NOT NULL,
  task_id    TEXT,
  type       TEXT NOT NULL,
  payload    TEXT NOT NULL DEFAULT '{}',
  at         TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_events_project ON events(project_id, at);
