// tools/compliance-audit-handler.ts — SPEC-371: Compliance & Audit Logs (SOC2/ISO27001)
import { generateComplianceReport, exportAuditLog, analyzeComplianceGaps, } from '../engine/compliance-reporter.js';
import { loadRetentionPolicy, saveRetentionPolicy } from '../storage/retention-policy-store.js';
import { specStore } from '../storage/index.js';
import { hashProjectPath } from '../storage/base-store.js';
export function handleGenerateComplianceReport(input) {
    const format = input.outputFormat ?? 'json';
    const result = generateComplianceReport(input.projectPath, input.standard, format);
    return {
        content: [
            {
                type: 'text',
                text: [
                    `Compliance report generated (${input.standard}, ${format}):`,
                    ``,
                    result.content,
                ].join('\n'),
            },
        ],
    };
}
export async function handleAuditLogExport(input) {
    const format = input.format ?? 'json';
    const projectId = hashProjectPath(input.projectPath);
    const specs = await specStore.listSpecs(projectId);
    const content = exportAuditLog(specs, format, input.dateRange);
    return {
        content: [
            {
                type: 'text',
                text: [`Audit log export (${format}):`, ``, content].join('\n'),
            },
        ],
    };
}
export async function handleDataRetentionPolicy(input) {
    const current = await loadRetentionPolicy(input.projectPath);
    const updated = {
        ...current,
        retentionDays: input.retentionDays,
        autoDelete: input.autoDelete ?? current.autoDelete,
        notifyBeforeDeleteDays: input.notifyBeforeDeleteDays ?? current.notifyBeforeDeleteDays,
    };
    await saveRetentionPolicy(input.projectPath, updated);
    const lines = [
        `Data retention policy saved:`,
        `  Retain data for: ${updated.retentionDays} days`,
        `  Auto-delete: ${updated.autoDelete ? 'enabled' : 'disabled'}`,
        `  Notify before delete: ${updated.notifyBeforeDeleteDays} days`,
        ``,
        `NOTE: Auto-delete does not execute automatically.`,
        `When enabled, it describes that data older than ${updated.retentionDays} days should be deleted.`,
        `Run audit_log_export to identify data subject to this policy.`,
    ];
    return {
        content: [{ type: 'text', text: lines.join('\n') }],
    };
}
export function handleComplianceGapAnalysis(input) {
    const gaps = analyzeComplianceGaps(input.standard);
    if (gaps.length === 0) {
        return {
            content: [
                {
                    type: 'text',
                    text: `No compliance gaps found for ${input.standard}. All controls satisfied.`,
                },
            ],
        };
    }
    const gapLines = gaps.map((g) => `• [${g.severity.toUpperCase()}] ${g.controlId} — ${g.controlName}\n` +
        `  Gap: ${g.gap}\n` +
        `  Recommendation: ${g.recommendation}`);
    return {
        content: [
            {
                type: 'text',
                text: [
                    `Compliance gap analysis for ${input.standard} (${gaps.length} gaps):`,
                    ``,
                    ...gapLines,
                ].join('\n'),
            },
        ],
    };
}
//# sourceMappingURL=compliance-audit-handler.js.map