/**
 * Audit log export — JSON and CSV formats (Pro feature).
 */
import { getDatabase } from '../../database/init.js';
function queryLogs(startTime, endTime) {
    const db = getDatabase();
    const conditions = [];
    const params = [];
    if (startTime) {
        conditions.push('timestamp >= ?');
        params.push(startTime);
    }
    if (endTime) {
        conditions.push('timestamp <= ?');
        params.push(endTime);
    }
    const where = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
    return db.prepare(`SELECT * FROM defence_audit ${where} ORDER BY timestamp DESC`).all(...params);
}
export function exportAuditJSON(startTime, endTime) {
    const logs = queryLogs(startTime, endTime);
    return JSON.stringify({ exported_at: new Date().toISOString(), count: logs.length, logs }, null, 2);
}
export function exportAuditCSV(startTime, endTime) {
    const logs = queryLogs(startTime, endTime);
    const headers = [
        'id', 'memory_id', 'project', 'timestamp', 'source_type', 'source_identifier',
        'trust_score', 'sensitivity_level', 'firewall_result', 'anomaly_score',
        'threat_indicators', 'blocked_patterns', 'reason', 'fragmentation_score', 'pipeline_duration_ms',
    ];
    const escapeCSV = (val) => {
        if (val === null || val === undefined)
            return '';
        const str = String(val);
        if (str.includes(',') || str.includes('"') || str.includes('\n')) {
            return `"${str.replace(/"/g, '""')}"`;
        }
        return str;
    };
    const rows = logs.map(row => headers.map(h => escapeCSV(row[h])).join(','));
    return [headers.join(','), ...rows].join('\n');
}
