-- Ignite-specific database schema extensions
-- These tables extend Claude-Mem's base schema for Ignite workflow tracking

-- Projects table
CREATE TABLE IF NOT EXISTS projects (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT UNIQUE NOT NULL,
  path TEXT NOT NULL,
  description TEXT,
  created_at TEXT NOT NULL,
  created_at_epoch INTEGER NOT NULL,
  updated_at TEXT NOT NULL,
  updated_at_epoch INTEGER NOT NULL,
  current_milestone TEXT,
  current_phase INTEGER,
  status TEXT CHECK(status IN ('active', 'paused', 'completed')) DEFAULT 'active',
  metadata TEXT -- JSON
);

CREATE INDEX IF NOT EXISTS idx_projects_name ON projects(name);
CREATE INDEX IF NOT EXISTS idx_projects_status ON projects(status);
CREATE INDEX IF NOT EXISTS idx_projects_updated ON projects(updated_at_epoch DESC);

-- Phases table
CREATE TABLE IF NOT EXISTS phases (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  project_id INTEGER NOT NULL,
  phase_number INTEGER NOT NULL,
  name TEXT NOT NULL,
  goal TEXT,
  status TEXT CHECK(status IN ('planned', 'researching', 'planning', 'executing', 'verifying', 'completed')) DEFAULT 'planned',
  requirements TEXT, -- JSON array of REQ-IDs
  success_criteria TEXT, -- JSON array
  started_at TEXT,
  started_at_epoch INTEGER,
  completed_at TEXT,
  completed_at_epoch INTEGER,
  FOREIGN KEY(project_id) REFERENCES projects(id) ON DELETE CASCADE,
  UNIQUE(project_id, phase_number)
);

CREATE INDEX IF NOT EXISTS idx_phases_project ON phases(project_id);
CREATE INDEX IF NOT EXISTS idx_phases_status ON phases(status);
CREATE INDEX IF NOT EXISTS idx_phases_number ON phases(project_id, phase_number);

-- Agent executions table
CREATE TABLE IF NOT EXISTS agent_runs (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  project_id INTEGER,
  phase_id INTEGER,
  session_id TEXT NOT NULL,
  agent_type TEXT NOT NULL CHECK(agent_type IN (
    'planner', 'executor', 'verifier', 'researcher',
    'debugger', 'roadmapper', 'integration-checker',
    'phase-researcher', 'project-researcher', 'codebase-mapper'
  )),
  command TEXT, -- The ignite command that triggered this
  input_prompt TEXT NOT NULL,
  output_response TEXT,
  observations_captured TEXT, -- JSON array
  token_count INTEGER,
  execution_time_ms INTEGER,
  status TEXT CHECK(status IN ('running', 'completed', 'failed')) DEFAULT 'running',
  error_message TEXT,
  created_at TEXT NOT NULL,
  created_at_epoch INTEGER NOT NULL,
  completed_at TEXT,
  completed_at_epoch INTEGER,
  FOREIGN KEY(project_id) REFERENCES projects(id) ON DELETE CASCADE,
  FOREIGN KEY(phase_id) REFERENCES phases(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_agent_runs_project ON agent_runs(project_id);
CREATE INDEX IF NOT EXISTS idx_agent_runs_phase ON agent_runs(phase_id);
CREATE INDEX IF NOT EXISTS idx_agent_runs_session ON agent_runs(session_id);
CREATE INDEX IF NOT EXISTS idx_agent_runs_type ON agent_runs(agent_type);
CREATE INDEX IF NOT EXISTS idx_agent_runs_created ON agent_runs(created_at_epoch DESC);

-- Requirements tracking table
CREATE TABLE IF NOT EXISTS requirements (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  project_id INTEGER NOT NULL,
  req_id TEXT NOT NULL, -- e.g., AUTH-01
  category TEXT NOT NULL, -- e.g., Authentication
  description TEXT NOT NULL,
  status TEXT CHECK(status IN ('active', 'validated', 'deferred', 'removed')) DEFAULT 'active',
  phase_id INTEGER, -- Which phase implements this
  validation_criteria TEXT,
  validated_at TEXT,
  validated_at_epoch INTEGER,
  created_at TEXT NOT NULL,
  created_at_epoch INTEGER NOT NULL,
  FOREIGN KEY(project_id) REFERENCES projects(id) ON DELETE CASCADE,
  FOREIGN KEY(phase_id) REFERENCES phases(id) ON DELETE SET NULL,
  UNIQUE(project_id, req_id)
);

CREATE INDEX IF NOT EXISTS idx_requirements_project ON requirements(project_id);
CREATE INDEX IF NOT EXISTS idx_requirements_phase ON requirements(phase_id);
CREATE INDEX IF NOT EXISTS idx_requirements_status ON requirements(status);
CREATE INDEX IF NOT EXISTS idx_requirements_req_id ON requirements(project_id, req_id);

-- Workflow preferences table
CREATE TABLE IF NOT EXISTS workflow_configs (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  project_id INTEGER NOT NULL,
  mode TEXT CHECK(mode IN ('yolo', 'interactive')) DEFAULT 'yolo',
  depth TEXT CHECK(depth IN ('quick', 'standard', 'comprehensive')) DEFAULT 'standard',
  parallelization BOOLEAN DEFAULT 1,
  commit_docs BOOLEAN DEFAULT 1,
  model_profile TEXT CHECK(model_profile IN ('quality', 'balanced', 'budget')) DEFAULT 'balanced',
  research_enabled BOOLEAN DEFAULT 1,
  plan_check_enabled BOOLEAN DEFAULT 1,
  verifier_enabled BOOLEAN DEFAULT 1,
  created_at TEXT NOT NULL,
  created_at_epoch INTEGER NOT NULL,
  FOREIGN KEY(project_id) REFERENCES projects(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_workflow_configs_project ON workflow_configs(project_id);

-- Checkpoints table (for crash recovery)
CREATE TABLE IF NOT EXISTS checkpoints (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  project_id INTEGER NOT NULL,
  phase_id INTEGER,
  checkpoint_type TEXT NOT NULL CHECK(checkpoint_type IN (
    'phase_start', 'phase_plan', 'phase_execute', 'phase_verify', 'phase_complete',
    'debug_start', 'debug_hypothesis', 'debug_test', 'debug_complete'
  )),
  state_data TEXT NOT NULL, -- JSON snapshot
  created_at TEXT NOT NULL,
  created_at_epoch INTEGER NOT NULL,
  restored_at TEXT,
  restored_at_epoch INTEGER,
  FOREIGN KEY(project_id) REFERENCES projects(id) ON DELETE CASCADE,
  FOREIGN KEY(phase_id) REFERENCES phases(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_checkpoints_project ON checkpoints(project_id);
CREATE INDEX IF NOT EXISTS idx_checkpoints_phase ON checkpoints(phase_id);
CREATE INDEX IF NOT EXISTS idx_checkpoints_type ON checkpoints(checkpoint_type);
CREATE INDEX IF NOT EXISTS idx_checkpoints_created ON checkpoints(created_at_epoch DESC);

-- Patterns table (learned patterns and anti-patterns)
CREATE TABLE IF NOT EXISTS patterns (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  project_id INTEGER,
  pattern_type TEXT CHECK(pattern_type IN ('pattern', 'antipattern', 'preference')) NOT NULL,
  category TEXT, -- e.g., 'architecture', 'testing', 'style'
  title TEXT NOT NULL,
  description TEXT,
  examples TEXT, -- JSON array
  frequency INTEGER DEFAULT 1,
  confidence REAL DEFAULT 0.5,
  last_seen TEXT,
  last_seen_epoch INTEGER,
  created_at TEXT NOT NULL,
  created_at_epoch INTEGER NOT NULL,
  FOREIGN KEY(project_id) REFERENCES projects(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_patterns_project ON patterns(project_id);
CREATE INDEX IF NOT EXISTS idx_patterns_type ON patterns(pattern_type);
CREATE INDEX IF NOT EXISTS idx_patterns_category ON patterns(category);
CREATE INDEX IF NOT EXISTS idx_patterns_confidence ON patterns(confidence DESC);

-- Decisions table (architectural and technical decisions)
CREATE TABLE IF NOT EXISTS decisions (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  project_id INTEGER NOT NULL,
  phase_id INTEGER,
  decision_type TEXT CHECK(decision_type IN ('architecture', 'technical', 'process', 'scope')) NOT NULL,
  title TEXT NOT NULL,
  choice TEXT NOT NULL,
  rationale TEXT,
  alternatives_considered TEXT, -- JSON array
  outcome TEXT,
  outcome_status TEXT CHECK(outcome_status IN ('pending', 'successful', 'failed', 'mixed')),
  created_at TEXT NOT NULL,
  created_at_epoch INTEGER NOT NULL,
  FOREIGN KEY(project_id) REFERENCES projects(id) ON DELETE CASCADE,
  FOREIGN KEY(phase_id) REFERENCES phases(id) ON DELETE SET NULL
);

CREATE INDEX IF NOT EXISTS idx_decisions_project ON decisions(project_id);
CREATE INDEX IF NOT EXISTS idx_decisions_phase ON decisions(phase_id);
CREATE INDEX IF NOT EXISTS idx_decisions_type ON decisions(decision_type);
CREATE INDEX IF NOT EXISTS idx_decisions_created ON decisions(created_at_epoch DESC);

-- Migrations tracking
CREATE TABLE IF NOT EXISTS ignite_migrations (
  id INTEGER PRIMARY KEY,
  version INTEGER UNIQUE NOT NULL,
  name TEXT NOT NULL,
  applied_at TEXT NOT NULL,
  applied_at_epoch INTEGER NOT NULL
);

-- Insert initial migration record
INSERT OR IGNORE INTO ignite_migrations (version, name, applied_at, applied_at_epoch)
VALUES (1, 'initial_ignite_schema', datetime('now'), strftime('%s', 'now') * 1000);