/**
 * Control State Module
 *
 * Manages global control state for the ShieldCortex system.
 * Handles pause/resume for memory creation and kill switch lockdown.
 *
 * Kill switch is a stronger state than pause — it blocks ALL operations
 * except forensic tools (audit, status, scan, resume).
 */
import { logIronDomeAudit } from '../defence/iron-dome/audit.js';
export class KillSwitchError extends Error {
    meta;
    constructor(meta) {
        super('Kill switch active — operation blocked');
        this.meta = meta;
        this.name = 'KillSwitchError';
    }
}
// ── Lockdown Policy ──
const ALLOWED_DURING_LOCKDOWN = new Set([
    'status',
    'audit',
    'scan',
    'resume',
    'emergency_stop',
]);
// ── State ──
const startTime = Date.now();
let mode = 'active';
let killSwitchMeta = null;
// ── Pause (soft — memory writes only) ──
export function isPaused() {
    return mode === 'paused' || mode === 'kill_switch';
}
export function pause() {
    if (mode === 'kill_switch')
        return; // kill switch takes precedence
    mode = 'paused';
    console.log('[shieldcortex] Memory creation PAUSED');
}
export function resume() {
    if (mode === 'kill_switch')
        return; // must use deactivateKillSwitch
    mode = 'active';
    console.log('[shieldcortex] Memory creation RESUMED');
}
// ── Kill Switch (hard — blocks everything except forensics) ──
export function isKillSwitchActive() {
    return mode === 'kill_switch';
}
export function getKillSwitchMeta() {
    return killSwitchMeta;
}
export function activateKillSwitch(meta) {
    if (mode === 'kill_switch')
        return; // already active, idempotent
    const fullMeta = {
        ...meta,
        triggeredAt: new Date().toISOString(),
    };
    mode = 'kill_switch';
    killSwitchMeta = fullMeta;
    console.log(`[shieldcortex] KILL SWITCH ACTIVATED — source: ${meta.source}${meta.phrase ? `, phrase: "${meta.phrase}"` : ''}`);
    // Audit log
    try {
        logIronDomeAudit({
            action: 'kill_switch_activated',
            allowed: false,
            reason: `Kill switch activated. Source: ${meta.source}.${meta.phrase ? ` Phrase: "${meta.phrase}".` : ''}${meta.reason ? ` Reason: ${meta.reason}.` : ''} Memories: ${meta.memoryCountAtTrigger ?? 'unknown'}.`,
        });
    }
    catch {
        // Audit logging must never break kill switch activation
    }
    // Cross-process event + cloud alert are wired via events.ts
    // Import lazily to avoid circular deps
    try {
        import('../api/events.js').then(({ emitKillSwitchActivated }) => {
            emitKillSwitchActivated(fullMeta);
        }).catch(() => { });
    }
    catch {
        // Best-effort
    }
    try {
        import('../cloud/sync.js').then(({ sendKillSwitchAlert }) => {
            sendKillSwitchAlert(fullMeta);
        }).catch(() => { });
    }
    catch {
        // Best-effort
    }
}
export function deactivateKillSwitch(reason) {
    if (mode !== 'kill_switch')
        return;
    const previousMeta = killSwitchMeta;
    mode = 'active';
    killSwitchMeta = null;
    console.log(`[shieldcortex] Kill switch deactivated — reason: ${reason}`);
    try {
        logIronDomeAudit({
            action: 'kill_switch_deactivated',
            allowed: true,
            reason: `Kill switch deactivated. Reason: ${reason}. Was active since: ${previousMeta?.triggeredAt ?? 'unknown'}.`,
        });
    }
    catch {
        // Audit logging must never break deactivation
    }
    try {
        import('../api/events.js').then(({ emitKillSwitchDeactivated }) => {
            emitKillSwitchDeactivated({ reason, previousMeta });
        }).catch(() => { });
    }
    catch {
        // Best-effort
    }
}
// ── Central Policy Enforcement ──
/**
 * Assert that an operation is allowed under the current control mode.
 * Throws KillSwitchError if the operation is blocked.
 */
export function assertOperationAllowed(kind) {
    if (mode !== 'kill_switch')
        return;
    if (ALLOWED_DURING_LOCKDOWN.has(kind))
        return;
    throw new KillSwitchError(killSwitchMeta);
}
// ── Status ──
export function getControlStatus() {
    const uptime = Date.now() - startTime;
    const seconds = Math.floor(uptime / 1000);
    const minutes = Math.floor(seconds / 60);
    const hours = Math.floor(minutes / 60);
    const days = Math.floor(hours / 24);
    let uptimeFormatted;
    if (days > 0) {
        uptimeFormatted = `${days}d ${hours % 24}h ${minutes % 60}m`;
    }
    else if (hours > 0) {
        uptimeFormatted = `${hours}h ${minutes % 60}m ${seconds % 60}s`;
    }
    else if (minutes > 0) {
        uptimeFormatted = `${minutes}m ${seconds % 60}s`;
    }
    else {
        uptimeFormatted = `${seconds}s`;
    }
    return {
        mode,
        paused: isPaused(),
        killSwitchActive: isKillSwitchActive(),
        killSwitchMeta,
        uptime,
        uptimeFormatted,
    };
}
