"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.auditAdapter = exports.ConsoleAuditAdapter = exports.FileAuditAdapter = void 0;
exports.createAuditAdapter = createAuditAdapter;
class FileAuditAdapter {
    constructor(logFile = 'sentinel_audit.log') {
        this.logFile = logFile;
        try {
            this.fs = require('fs').promises;
        }
        catch (error) {
            console.warn('File system not available, falling back to console audit');
            this.fs = null;
        }
    }
    async logAudit(record) {
        const logEntry = this.formatLogEntry(record);
        if (this.fs) {
            try {
                await this.fs.appendFile(this.logFile, logEntry + '\n');
            }
            catch (error) {
                console.error('Failed to write to audit log:', error);
                console.log('Audit entry:', logEntry);
            }
        }
        else {
            console.log('Audit entry:', logEntry);
        }
    }
    async logKill(runId, reason, config, metadata) {
        const record = {
            id: this.generateId(),
            timestamp: Date.now(),
            action: 'kill',
            runId,
            reason,
            config,
            ...(metadata !== undefined && { metadata })
        };
        await this.logAudit(record);
    }
    async logPolicyViolation(runId, policy, threshold, config, metadata) {
        const record = {
            id: this.generateId(),
            timestamp: Date.now(),
            action: 'policy_violation',
            runId,
            reason: `Policy ${policy} exceeded threshold ${threshold}`,
            config,
            metadata: {
                policy,
                threshold,
                ...metadata
            }
        };
        await this.logAudit(record);
    }
    formatLogEntry(record) {
        return JSON.stringify({
            ...record,
            timestamp: new Date(record.timestamp).toISOString()
        });
    }
    generateId() {
        return `audit_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
    }
}
exports.FileAuditAdapter = FileAuditAdapter;
class ConsoleAuditAdapter {
    async logAudit(record) {
        console.log('🔒 Sentinel Audit:', {
            ...record,
            timestamp: new Date(record.timestamp).toISOString()
        });
    }
    async logKill(runId, reason, config, metadata) {
        const record = {
            id: this.generateId(),
            timestamp: Date.now(),
            action: 'kill',
            runId,
            reason,
            config,
            ...(metadata !== undefined && { metadata })
        };
        await this.logAudit(record);
    }
    async logPolicyViolation(runId, policy, threshold, config, metadata) {
        await this.logAudit({
            id: this.generateId(),
            timestamp: Date.now(),
            action: 'policy_violation',
            runId,
            reason: `Policy ${policy} exceeded threshold ${threshold}`,
            config,
            metadata: {
                policy,
                threshold,
                ...metadata
            }
        });
    }
    generateId() {
        return `audit_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
    }
}
exports.ConsoleAuditAdapter = ConsoleAuditAdapter;
function createAuditAdapter() {
    const sink = process.env['SENTINEL_AUDIT_SINK'];
    const file = process.env['SENTINEL_AUDIT_FILE'] || 'packages/sentinel/sentinel_audit.log';
    if (sink === 'file') {
        return new FileAuditAdapter(file);
    }
    return new ConsoleAuditAdapter();
}
exports.auditAdapter = createAuditAdapter();
//# sourceMappingURL=audit.js.map