/**
 * SQLite Phase Timing Repository Implementation
 *
 * Implements IPhaseTimingRepository using SQLite database.
 * Uses prepared statements to prevent SQL injection.
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { injectable } from 'tsyringe';
function toDatabase(timing) {
    return {
        id: timing.id,
        agent_run_id: timing.agentRunId,
        phase: timing.phase,
        started_at: timing.startedAt instanceof Date ? timing.startedAt.getTime() : timing.startedAt,
        completed_at: timing.completedAt instanceof Date ? timing.completedAt.getTime() : null,
        duration_ms: timing.durationMs != null ? Number(timing.durationMs) : null,
        created_at: timing.createdAt instanceof Date ? timing.createdAt.getTime() : timing.createdAt,
        updated_at: timing.updatedAt instanceof Date ? timing.updatedAt.getTime() : timing.updatedAt,
    };
}
function fromDatabase(row) {
    return {
        id: row.id,
        agentRunId: row.agent_run_id,
        phase: row.phase,
        startedAt: new Date(row.started_at),
        createdAt: new Date(row.created_at),
        updatedAt: new Date(row.updated_at),
        ...(row.completed_at !== null && { completedAt: new Date(row.completed_at) }),
        ...(row.duration_ms !== null && { durationMs: BigInt(row.duration_ms) }),
    };
}
/**
 * SQLite implementation of IPhaseTimingRepository.
 */
let SQLitePhaseTimingRepository = class SQLitePhaseTimingRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async save(phaseTiming) {
        const row = toDatabase(phaseTiming);
        const stmt = this.db.prepare(`
      INSERT INTO phase_timings (
        id, agent_run_id, phase, started_at, completed_at, duration_ms,
        created_at, updated_at
      ) VALUES (
        @id, @agent_run_id, @phase, @started_at, @completed_at, @duration_ms,
        @created_at, @updated_at
      )
    `);
        stmt.run(row);
    }
    async update(id, updates) {
        const setClauses = ['updated_at = @updated_at'];
        const params = {
            id,
            updated_at: Date.now(),
        };
        if (updates.completedAt !== undefined) {
            setClauses.push('completed_at = @completed_at');
            params.completed_at =
                updates.completedAt instanceof Date ? updates.completedAt.getTime() : updates.completedAt;
        }
        if (updates.durationMs !== undefined) {
            setClauses.push('duration_ms = @duration_ms');
            params.duration_ms = updates.durationMs;
        }
        const stmt = this.db.prepare(`UPDATE phase_timings SET ${setClauses.join(', ')} WHERE id = @id`);
        stmt.run(params);
    }
    async findByRunId(agentRunId) {
        const stmt = this.db.prepare('SELECT * FROM phase_timings WHERE agent_run_id = ? ORDER BY created_at');
        const rows = stmt.all(agentRunId);
        return rows.map(fromDatabase);
    }
    async findByFeatureId(featureId) {
        const stmt = this.db.prepare(`
      SELECT pt.* FROM phase_timings pt
      INNER JOIN agent_runs ar ON pt.agent_run_id = ar.id
      WHERE ar.feature_id = ?
      ORDER BY pt.created_at
    `);
        const rows = stmt.all(featureId);
        return rows.map(fromDatabase);
    }
};
SQLitePhaseTimingRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLitePhaseTimingRepository);
export { SQLitePhaseTimingRepository };
