/**
 * SQLite AgentMessage Repository (spec 093)
 *
 * Backed by the agent_messages table (migration 087). Every read is
 * scoped by app_id at the SQL layer for cross-app isolation (NFR-7).
 */
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-message.mapper.js';
let SQLiteAgentMessageRepository = class SQLiteAgentMessageRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async create(message) {
        const row = toDatabase(message);
        const stmt = this.db.prepare(`
      INSERT INTO agent_messages (
        id, app_id, feature_id, from_agent_run_id, from_actor,
        to_target, to_kind, message_kind, payload,
        correlation_id, delivered_at, created_at, updated_at
      ) VALUES (
        @id, @app_id, @feature_id, @from_agent_run_id, @from_actor,
        @to_target, @to_kind, @message_kind, @payload,
        @correlation_id, @delivered_at, @created_at, @updated_at
      )
    `);
        stmt.run(row);
    }
    async findById(appId, id) {
        const stmt = this.db.prepare('SELECT * FROM agent_messages WHERE id = ? AND app_id = ?');
        const row = stmt.get(id, appId);
        return row ? fromDatabase(row) : null;
    }
    async findByCorrelationId(appId, correlationId) {
        const stmt = this.db.prepare('SELECT * FROM agent_messages WHERE app_id = ? AND correlation_id = ? ORDER BY created_at ASC LIMIT 1');
        const row = stmt.get(appId, correlationId);
        return row ? fromDatabase(row) : null;
    }
    async listByScope(appId, featureId, filters = {}) {
        const conditions = ['app_id = ?'];
        const params = [appId];
        if (featureId !== undefined) {
            conditions.push('feature_id = ?');
            params.push(featureId);
        }
        if (filters.since) {
            conditions.push('created_at >= ?');
            params.push(filters.since.getTime());
        }
        if (filters.undeliveredOnly) {
            conditions.push('delivered_at IS NULL');
        }
        const limitClause = filters.limit !== undefined ? ' LIMIT ?' : '';
        if (filters.limit !== undefined) {
            params.push(filters.limit);
        }
        const sql = `SELECT * FROM agent_messages WHERE ${conditions.join(' AND ')} ORDER BY created_at ASC${limitClause}`;
        const rows = this.db.prepare(sql).all(...params);
        return rows.map(fromDatabase);
    }
    async markDelivered(appId, id, deliveredAt) {
        const now = Date.now();
        const stmt = this.db.prepare('UPDATE agent_messages SET delivered_at = ?, updated_at = ? WHERE id = ? AND app_id = ? AND delivered_at IS NULL');
        stmt.run(deliveredAt.getTime(), now, id, appId);
    }
};
SQLiteAgentMessageRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLiteAgentMessageRepository);
export { SQLiteAgentMessageRepository };
