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/pm-audit-log.mapper.js';
let SQLitePmAuditLogRepository = class SQLitePmAuditLogRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async create(entry) {
        const row = toDatabase(entry);
        const stmt = this.db.prepare(`
      INSERT INTO pm_audit_log (
        id, actor_id, action, target_id, target_type,
        metadata, ip_address, created_at, updated_at
      ) VALUES (
        @id, @actor_id, @action, @target_id, @target_type,
        @metadata, @ip_address, @created_at, @updated_at
      )
    `);
        stmt.run(row);
    }
    async list(filters) {
        const { sql, params } = this.buildFilterQuery('SELECT *', filters);
        const limitClause = filters?.limit ? ` LIMIT ${filters.limit}` : ' LIMIT 100';
        const offsetClause = filters?.offset ? ` OFFSET ${filters.offset}` : '';
        const stmt = this.db.prepare(`${sql} ORDER BY created_at DESC${limitClause}${offsetClause}`);
        const rows = stmt.all(...params);
        return rows.map(fromDatabase);
    }
    async count(filters) {
        const { sql, params } = this.buildFilterQuery('SELECT COUNT(*) as count', filters);
        const stmt = this.db.prepare(sql);
        const result = stmt.get(...params);
        return result.count;
    }
    buildFilterQuery(selectClause, filters) {
        const conditions = [];
        const params = [];
        if (filters?.actorId) {
            conditions.push('actor_id = ?');
            params.push(filters.actorId);
        }
        if (filters?.action) {
            conditions.push('action = ?');
            params.push(filters.action);
        }
        if (filters?.targetId) {
            conditions.push('target_id = ?');
            params.push(filters.targetId);
        }
        if (filters?.fromDate) {
            conditions.push('created_at >= ?');
            params.push(filters.fromDate.getTime());
        }
        if (filters?.toDate) {
            conditions.push('created_at <= ?');
            params.push(filters.toDate.getTime());
        }
        const whereClause = conditions.length > 0 ? ` WHERE ${conditions.join(' AND ')}` : '';
        return { sql: `${selectClause} FROM pm_audit_log${whereClause}`, params };
    }
};
SQLitePmAuditLogRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLitePmAuditLogRepository);
export { SQLitePmAuditLogRepository };
