// Planu — Per-project compliance audit event store (SPEC-402)
// Separate from audit-trail-store.ts which handles EU AI Act global audit
import { appendFile, readFile } from 'node:fs/promises';
import { existsSync, mkdirSync } from 'node:fs';
import { projectDataDir, safeJsonParse } from './base-store.js';
function auditFile(projectId) {
    return `${projectDataDir(projectId)}/compliance-audit-trail.jsonl`;
}
function ensureDir(projectId) {
    const dir = projectDataDir(projectId);
    if (!existsSync(dir)) {
        mkdirSync(dir, { recursive: true });
    }
}
export async function appendComplianceAuditEvent(event) {
    ensureDir(event.projectId);
    const line = JSON.stringify(event) + '\n';
    await appendFile(auditFile(event.projectId), line, 'utf-8');
}
export async function loadComplianceAuditTrail(projectId, limit = 100) {
    const file = auditFile(projectId);
    if (!existsSync(file)) {
        return [];
    }
    const content = await readFile(file, 'utf-8');
    const events = content
        .split('\n')
        .filter(Boolean)
        .map((line) => safeJsonParse(line, null))
        .filter((e) => e !== null);
    return events.slice(-limit);
}
export async function countComplianceAuditEvents(projectId) {
    const file = auditFile(projectId);
    if (!existsSync(file)) {
        return 0;
    }
    try {
        const content = await readFile(file, 'utf-8');
        return content.split('\n').filter(Boolean).length;
    }
    catch {
        return 0;
    }
}
//# sourceMappingURL=compliance-audit-store.js.map