/**
 * Database Migrations
 *
 * Schema migrations for the SQLite database.
 *
 * @module persistence/migrations
 */
export const MIGRATIONS = [
    {
        version: 1,
        name: "initial_schema",
        up: `
      -- Projects table
      CREATE TABLE IF NOT EXISTS projects (
        id TEXT PRIMARY KEY,
        path TEXT UNIQUE NOT NULL,
        name TEXT NOT NULL,
        created_at TEXT NOT NULL DEFAULT (datetime('now')),
        last_scan_at TEXT,
        metadata TEXT
      );

      CREATE INDEX IF NOT EXISTS idx_projects_path ON projects(path);

      -- Findings table
      CREATE TABLE IF NOT EXISTS findings (
        id TEXT PRIMARY KEY,
        project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
        certification_id TEXT,
        severity TEXT NOT NULL CHECK (severity IN ('critical', 'high', 'medium', 'low', 'info')),
        category TEXT NOT NULL,
        file TEXT NOT NULL,
        line INTEGER NOT NULL,
        column_num INTEGER,
        description TEXT NOT NULL,
        scanner_source TEXT NOT NULL,
        rule_id TEXT,
        status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'fixed', 'wontfix', 'false_positive', 'in_progress')),
        confidence INTEGER NOT NULL DEFAULT 100,
        cwe_ids TEXT,
        first_seen_at TEXT NOT NULL DEFAULT (datetime('now')),
        last_seen_at TEXT NOT NULL DEFAULT (datetime('now')),
        fixed_at TEXT,
        fixed_by TEXT,
        false_positive INTEGER DEFAULT 0,
        false_positive_reason TEXT,
        metadata TEXT
      );

      CREATE INDEX IF NOT EXISTS idx_findings_project ON findings(project_id);
      CREATE INDEX IF NOT EXISTS idx_findings_severity ON findings(severity);
      CREATE INDEX IF NOT EXISTS idx_findings_status ON findings(status);
      CREATE INDEX IF NOT EXISTS idx_findings_category ON findings(category);
      CREATE INDEX IF NOT EXISTS idx_findings_file ON findings(file);
      CREATE INDEX IF NOT EXISTS idx_findings_first_seen ON findings(first_seen_at);

      -- Scans table
      CREATE TABLE IF NOT EXISTS scans (
        id TEXT PRIMARY KEY,
        project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
        certification_id TEXT,
        started_at TEXT NOT NULL DEFAULT (datetime('now')),
        completed_at TEXT,
        status TEXT NOT NULL DEFAULT 'running' CHECK (status IN ('running', 'completed', 'failed', 'cancelled')),
        total_findings INTEGER DEFAULT 0,
        new_findings INTEGER DEFAULT 0,
        fixed_findings INTEGER DEFAULT 0,
        by_severity TEXT,
        by_scanner TEXT,
        duration INTEGER,
        error TEXT,
        metadata TEXT
      );

      CREATE INDEX IF NOT EXISTS idx_scans_project ON scans(project_id);
      CREATE INDEX IF NOT EXISTS idx_scans_started ON scans(started_at);
      CREATE INDEX IF NOT EXISTS idx_scans_status ON scans(status);

      -- Trends table (pre-aggregated for performance)
      CREATE TABLE IF NOT EXISTS trends (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
        period TEXT NOT NULL CHECK (period IN ('day', 'week', 'month')),
        period_start TEXT NOT NULL,
        period_end TEXT NOT NULL,
        findings_new INTEGER DEFAULT 0,
        findings_fixed INTEGER DEFAULT 0,
        findings_open INTEGER DEFAULT 0,
        mttr_hours REAL,
        by_severity TEXT,
        UNIQUE(project_id, period, period_start)
      );

      CREATE INDEX IF NOT EXISTS idx_trends_project_period ON trends(project_id, period, period_start);
    `,
        down: `
      DROP TABLE IF EXISTS trends;
      DROP TABLE IF EXISTS scans;
      DROP TABLE IF EXISTS findings;
      DROP TABLE IF EXISTS projects;
    `,
    },
    {
        version: 2,
        name: "add_fix_history",
        up: `
      -- Fix history for tracking individual fixes
      CREATE TABLE IF NOT EXISTS fix_history (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        finding_id TEXT NOT NULL REFERENCES findings(id) ON DELETE CASCADE,
        action TEXT NOT NULL CHECK (action IN ('fixed', 'reopened', 'marked_false_positive', 'unmarked_false_positive')),
        performed_at TEXT NOT NULL DEFAULT (datetime('now')),
        performed_by TEXT,
        notes TEXT
      );

      CREATE INDEX IF NOT EXISTS idx_fix_history_finding ON fix_history(finding_id);
      CREATE INDEX IF NOT EXISTS idx_fix_history_performed ON fix_history(performed_at);
    `,
        down: `
      DROP TABLE IF EXISTS fix_history;
    `,
    },
];
//# sourceMappingURL=index.js.map