/**
 * 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';
/** Convert a Date, ISO string, or number to a Unix timestamp (ms). Returns null for invalid input. */
function toTimestamp(value) {
    if (value instanceof Date)
        return value.getTime();
    if (typeof value === 'number')
        return value;
    if (typeof value === 'string') {
        const ms = new Date(value).getTime();
        return Number.isNaN(ms) ? null : ms;
    }
    return null;
}
/**
 * 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,
        feature_id, repository_path,
        created_at, updated_at,
        approval_gates, model_id
      ) VALUES (
        @id, @agent_type, @agent_name, @status, @prompt, @result,
        @session_id, @thread_id, @pid, @last_heartbeat,
        @started_at, @completed_at, @error,
        @feature_id, @repository_path,
        @created_at, @updated_at,
        @approval_gates, @model_id
      )
    `);
        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 findByIds(ids) {
        if (ids.length === 0)
            return [];
        // SQLite's default SQLITE_MAX_VARIABLE_NUMBER is 32766 in modern builds,
        // but better-sqlite3 still defaults to 999. Chunk conservatively at 500
        // so the same code works regardless of the underlying limit.
        const CHUNK_SIZE = 500;
        const out = [];
        for (let i = 0; i < ids.length; i += CHUNK_SIZE) {
            const chunk = ids.slice(i, i + CHUNK_SIZE);
            const placeholders = chunk.map(() => '?').join(',');
            const stmt = this.db.prepare(`SELECT * FROM agent_runs WHERE id IN (${placeholders})`);
            const rows = stmt.all(...chunk);
            for (const row of rows) {
                out.push(fromDatabase(row));
            }
        }
        return out;
    }
    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: toTimestamp(updates?.updatedAt) ?? 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 = toTimestamp(updates.lastHeartbeat);
        }
        if (updates?.startedAt !== undefined) {
            setClauses.push('started_at = @started_at');
            params.started_at = toTimestamp(updates.startedAt);
        }
        if (updates?.completedAt !== undefined) {
            setClauses.push('completed_at = @completed_at');
            params.completed_at = toTimestamp(updates.completedAt);
        }
        if (updates?.error !== undefined) {
            setClauses.push('error = @error');
            params.error = updates.error;
        }
        if (updates?.approvalGates !== undefined) {
            setClauses.push('approval_gates = @approval_gates');
            params.approval_gates = JSON.stringify(updates.approvalGates);
        }
        const stmt = this.db.prepare(`UPDATE agent_runs SET ${setClauses.join(', ')} WHERE id = @id`);
        stmt.run(params);
    }
    async updatePinnedConfig(id, updates) {
        const stmt = this.db.prepare(`
      UPDATE agent_runs
      SET agent_type = @agent_type,
          model_id = @model_id,
          updated_at = @updated_at
      WHERE id = @id
    `);
        stmt.run({
            id,
            agent_type: updates.agentType,
            model_id: updates.modelId ?? null,
            updated_at: toTimestamp(updates.updatedAt) ?? Date.now(),
        });
    }
    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 };
