/**
 * SQLite Operation Log Repository
 *
 * Implements IOperationLogRepository using better-sqlite3. Append-only;
 * scoped reads by (operationKind, operationId).
 */
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 { randomUUID } from 'node:crypto';
import { injectable } from 'tsyringe';
function rowToEntry(row) {
    return {
        id: row.id,
        operationKind: row.operation_kind,
        operationId: row.operation_id,
        level: row.level,
        message: row.message,
        detail: row.detail ?? undefined,
        createdAt: new Date(row.created_at),
        updatedAt: new Date(row.updated_at),
    };
}
let SQLiteOperationLogRepository = class SQLiteOperationLogRepository {
    db;
    bus;
    constructor(db, bus) {
        this.db = db;
        this.bus = bus;
    }
    async append(input) {
        const id = randomUUID();
        const now = Date.now();
        this.db
            .prepare(`INSERT INTO operation_log_entries (
           id, operation_kind, operation_id, level, message, detail, created_at, updated_at
         ) VALUES (
           @id, @operation_kind, @operation_id, @level, @message, @detail, @created_at, @updated_at
         )`)
            .run({
            id,
            operation_kind: input.operationKind,
            operation_id: input.operationId,
            level: input.level,
            message: input.message,
            detail: input.detail ?? null,
            created_at: now,
            updated_at: now,
        });
        const entry = {
            id,
            operationKind: input.operationKind,
            operationId: input.operationId,
            level: input.level,
            message: input.message,
            detail: input.detail,
            createdAt: new Date(now),
            updatedAt: new Date(now),
        };
        this.bus.publish({ entry });
        return entry;
    }
    async listByScope(operationKind, operationId) {
        // Tiebreak by SQLite's built-in rowid (monotonic per insert) so entries
        // appended in the same millisecond still read back in insertion order.
        // Random UUID `id` sorts alphabetically, which is NOT insertion order.
        const rows = this.db
            .prepare(`SELECT id, operation_kind, operation_id, level, message, detail, created_at, updated_at
           FROM operation_log_entries
          WHERE operation_kind = ? AND operation_id = ?
          ORDER BY created_at ASC, rowid ASC`)
            .all(operationKind, operationId);
        return rows.map(rowToEntry);
    }
    async pruneBefore(timestamp) {
        const result = this.db
            .prepare('DELETE FROM operation_log_entries WHERE created_at < ?')
            .run(timestamp);
        return result.changes;
    }
};
SQLiteOperationLogRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object, Object])
], SQLiteOperationLogRepository);
export { SQLiteOperationLogRepository };
