/**
 * Scans Repository
 *
 * CRUD operations for scan history.
 *
 * @module persistence/repositories/scans
 */
import { randomUUID } from "crypto";
export class ScansRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    create(projectId, certificationId) {
        const id = randomUUID();
        const startedAt = new Date().toISOString();
        this.db
            .prepare(`INSERT INTO scans (id, project_id, certification_id, started_at, status)
         VALUES (?, ?, ?, ?, 'running')`)
            .run(id, projectId, certificationId || null, startedAt);
        return {
            id,
            projectId,
            certificationId,
            startedAt,
            status: "running",
            totalFindings: 0,
            newFindings: 0,
            fixedFindings: 0,
            bySeverity: { critical: 0, high: 0, medium: 0, low: 0, info: 0 },
            byScanner: {},
        };
    }
    complete(id, results) {
        this.db
            .prepare(`UPDATE scans SET
          completed_at = datetime('now'),
          status = 'completed',
          total_findings = ?,
          new_findings = ?,
          fixed_findings = ?,
          by_severity = ?,
          by_scanner = ?,
          duration = ?
         WHERE id = ?`)
            .run(results.totalFindings, results.newFindings, results.fixedFindings, JSON.stringify(results.bySeverity), JSON.stringify(results.byScanner), results.duration, id);
    }
    fail(id, error) {
        this.db
            .prepare(`UPDATE scans SET
          completed_at = datetime('now'),
          status = 'failed',
          error = ?
         WHERE id = ?`)
            .run(error, id);
    }
    findById(id) {
        const row = this.db
            .prepare("SELECT * FROM scans WHERE id = ?")
            .get(id);
        return row ? this.mapRow(row) : undefined;
    }
    findByProject(projectId, limit = 50) {
        const rows = this.db
            .prepare(`SELECT * FROM scans WHERE project_id = ?
         ORDER BY started_at DESC LIMIT ?`)
            .all(projectId, limit);
        return rows.map((r) => this.mapRow(r));
    }
    findRecent(limit = 100) {
        const rows = this.db
            .prepare(`SELECT * FROM scans ORDER BY started_at DESC LIMIT ?`)
            .all(limit);
        return rows.map((r) => this.mapRow(r));
    }
    getCompletedCount(projectId, sinceDate) {
        let sql = "SELECT COUNT(*) as count FROM scans WHERE project_id = ? AND status = 'completed'";
        const params = [projectId];
        if (sinceDate) {
            sql += " AND started_at >= ?";
            params.push(sinceDate);
        }
        const result = this.db.prepare(sql).get(...params);
        return result?.count || 0;
    }
    mapRow(row) {
        return {
            id: row.id,
            projectId: row.project_id,
            certificationId: row.certification_id || undefined,
            startedAt: row.started_at,
            completedAt: row.completed_at || undefined,
            status: row.status,
            totalFindings: row.total_findings || 0,
            newFindings: row.new_findings || 0,
            fixedFindings: row.fixed_findings || 0,
            bySeverity: row.by_severity
                ? JSON.parse(row.by_severity)
                : { critical: 0, high: 0, medium: 0, low: 0, info: 0 },
            byScanner: row.by_scanner ? JSON.parse(row.by_scanner) : {},
            duration: row.duration || undefined,
            error: row.error || undefined,
            metadata: row.metadata ? JSON.parse(row.metadata) : undefined,
        };
    }
}
//# sourceMappingURL=scans.js.map