/**
 * SQLite SupervisorDecision Repository (spec 093)
 *
 * Backed by the supervisor_decisions table (migration 090).
 * Append-only — no `update`, no `delete`.
 */
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/supervisor-decision.mapper.js';
let SQLiteSupervisorDecisionRepository = class SQLiteSupervisorDecisionRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async create(decision) {
        const row = toDatabase(decision);
        const stmt = this.db.prepare(`
      INSERT INTO supervisor_decisions (
        id, scope_type, scope_id, feature_id, supervisor_run_id,
        source_event_kind, source_event_id, verdict, rationale,
        model_id, prompt_version, rule_ref, confidence,
        created_at, updated_at
      ) VALUES (
        @id, @scope_type, @scope_id, @feature_id, @supervisor_run_id,
        @source_event_kind, @source_event_id, @verdict, @rationale,
        @model_id, @prompt_version, @rule_ref, @confidence,
        @created_at, @updated_at
      )
    `);
        stmt.run(row);
    }
    async findById(id) {
        const row = this.db.prepare('SELECT * FROM supervisor_decisions WHERE id = ?').get(id);
        return row ? fromDatabase(row) : null;
    }
    async listBySourceEvent(sourceEventKind, sourceEventId) {
        const rows = this.db
            .prepare('SELECT * FROM supervisor_decisions WHERE source_event_kind = ? AND source_event_id = ? ORDER BY created_at ASC')
            .all(sourceEventKind, sourceEventId);
        return rows.map(fromDatabase);
    }
    async listBySupervisorRun(supervisorRunId) {
        const rows = this.db
            .prepare('SELECT * FROM supervisor_decisions WHERE supervisor_run_id = ? ORDER BY created_at ASC')
            .all(supervisorRunId);
        return rows.map(fromDatabase);
    }
    async listByScope(scopeType, scopeId, featureId, filters = {}) {
        const conditions = ['scope_type = ?', "COALESCE(scope_id, '') = COALESCE(?, '')"];
        const params = [scopeType, scopeId ?? null];
        if (featureId !== undefined) {
            conditions.push('feature_id = ?');
            params.push(featureId);
        }
        if (filters.since) {
            conditions.push('created_at >= ?');
            params.push(filters.since.getTime());
        }
        const limitClause = filters.limit !== undefined ? ' LIMIT ?' : '';
        if (filters.limit !== undefined)
            params.push(filters.limit);
        const sql = `SELECT * FROM supervisor_decisions WHERE ${conditions.join(' AND ')} ORDER BY created_at DESC${limitClause}`;
        const rows = this.db.prepare(sql).all(...params);
        return rows.map(fromDatabase);
    }
    async listRecent(limit) {
        const safeLimit = Math.max(0, Math.floor(limit));
        const rows = this.db
            .prepare('SELECT * FROM supervisor_decisions ORDER BY created_at DESC LIMIT ?')
            .all(safeLimit);
        return rows.map(fromDatabase);
    }
};
SQLiteSupervisorDecisionRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLiteSupervisorDecisionRepository);
export { SQLiteSupervisorDecisionRepository };
