/**
 * SQLite Agent Run Repository Implementation
 *
 * Implements IAgentRunRepository 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';
import { toDatabase, fromDatabase, } from '../persistence/sqlite/mappers/agent-run.mapper.js';
/**
 * SQLite implementation of IAgentRunRepository.
 * Manages AgentRun persistence with CRUD operations.
 */
let SQLiteAgentRunRepository = class SQLiteAgentRunRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async create(agentRun) {
        const row = toDatabase(agentRun);
        const stmt = this.db.prepare(`
      INSERT INTO agent_runs (
        id, agent_type, agent_name, status, prompt, result,
        session_id, thread_id, pid, last_heartbeat,
        started_at, completed_at, error,
        created_at, updated_at
      ) VALUES (
        @id, @agent_type, @agent_name, @status, @prompt, @result,
        @session_id, @thread_id, @pid, @last_heartbeat,
        @started_at, @completed_at, @error,
        @created_at, @updated_at
      )
    `);
        stmt.run(row);
    }
    async findById(id) {
        const stmt = this.db.prepare('SELECT * FROM agent_runs WHERE id = ?');
        const row = stmt.get(id);
        if (!row) {
            return null;
        }
        return fromDatabase(row);
    }
    async findByThreadId(threadId) {
        const stmt = this.db.prepare('SELECT * FROM agent_runs WHERE thread_id = ?');
        const row = stmt.get(threadId);
        if (!row) {
            return null;
        }
        return fromDatabase(row);
    }
    async updateStatus(id, status, updates) {
        const setClauses = ['status = @status', 'updated_at = @updated_at'];
        const params = {
            id,
            status,
            updated_at: updates?.updatedAt instanceof Date ? updates.updatedAt.getTime() : Date.now(),
        };
        if (updates?.result !== undefined) {
            setClauses.push('result = @result');
            params.result = updates.result;
        }
        if (updates?.sessionId !== undefined) {
            setClauses.push('session_id = @session_id');
            params.session_id = updates.sessionId;
        }
        if (updates?.pid !== undefined) {
            setClauses.push('pid = @pid');
            params.pid = updates.pid;
        }
        if (updates?.lastHeartbeat !== undefined) {
            setClauses.push('last_heartbeat = @last_heartbeat');
            params.last_heartbeat =
                updates.lastHeartbeat instanceof Date ? updates.lastHeartbeat.getTime() : null;
        }
        if (updates?.startedAt !== undefined) {
            setClauses.push('started_at = @started_at');
            params.started_at = updates.startedAt instanceof Date ? updates.startedAt.getTime() : null;
        }
        if (updates?.completedAt !== undefined) {
            setClauses.push('completed_at = @completed_at');
            params.completed_at =
                updates.completedAt instanceof Date ? updates.completedAt.getTime() : null;
        }
        if (updates?.error !== undefined) {
            setClauses.push('error = @error');
            params.error = updates.error;
        }
        const stmt = this.db.prepare(`UPDATE agent_runs SET ${setClauses.join(', ')} WHERE id = @id`);
        stmt.run(params);
    }
    async findRunningByPid(pid) {
        const stmt = this.db.prepare('SELECT * FROM agent_runs WHERE pid = ? AND status = ?');
        const rows = stmt.all(pid, 'running');
        return rows.map(fromDatabase);
    }
    async list() {
        const stmt = this.db.prepare('SELECT * FROM agent_runs');
        const rows = stmt.all();
        return rows.map(fromDatabase);
    }
    async delete(id) {
        const stmt = this.db.prepare('DELETE FROM agent_runs WHERE id = ?');
        stmt.run(id);
    }
};
SQLiteAgentRunRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLiteAgentRunRepository);
export { SQLiteAgentRunRepository };
