/**
 * Nexus Prime — Verification Engine
 *
 * Sovereign verification: independent of execution.
 * A run's execution can complete, but verification is a separate gate.
 *
 * Produces VerificationProof objects with confirmed/flagged/rejected verdicts.
 */
export class VerificationEngine {
    /**
     * Run verification checks on worker results.
     * Returns a VerificationProof with verdict.
     */
    verify(workerResults) {
        const checks = [];
        const contradictions = [];
        // Check 1: all workers completed
        const failedWorkers = workerResults.filter(w => w.errors.length > 0);
        checks.push({
            name: 'worker_completion',
            passed: failedWorkers.length === 0,
            detail: failedWorkers.length === 0
                ? 'All workers completed without errors'
                : `${failedWorkers.length} worker(s) had errors`,
            severity: failedWorkers.length === 0 ? 'info' : 'error',
        });
        // Check 2: at least one verifier confirmed
        const verifiers = workerResults.filter(w => w.role === 'verifier');
        const verifiedCount = verifiers.filter(w => w.verified).length;
        checks.push({
            name: 'verifier_confirmation',
            passed: verifiers.length === 0 || verifiedCount > 0,
            detail: verifiers.length === 0
                ? 'No verifier workers in this run'
                : `${verifiedCount}/${verifiers.length} verifiers confirmed`,
            severity: verifiedCount > 0 ? 'info' : 'warning',
        });
        // Check 3: no contradicting modifications (same file modified by multiple workers)
        const fileModCounts = new Map();
        for (const w of workerResults) {
            for (const f of w.modifiedFiles) {
                const existing = fileModCounts.get(f) ?? [];
                existing.push(w.role);
                fileModCounts.set(f, existing);
            }
        }
        for (const [file, roles] of fileModCounts) {
            if (roles.length > 1) {
                contradictions.push(`${file} modified by: ${roles.join(', ')}`);
            }
        }
        checks.push({
            name: 'no_contradictions',
            passed: contradictions.length === 0,
            detail: contradictions.length === 0
                ? 'No file modification contradictions'
                : `${contradictions.length} file(s) modified by multiple workers`,
            severity: contradictions.length === 0 ? 'info' : 'warning',
        });
        // Determine verdict
        const hasErrors = checks.some(c => !c.passed && c.severity === 'error');
        const hasWarnings = checks.some(c => !c.passed && c.severity === 'warning');
        let verdict;
        if (hasErrors) {
            verdict = 'rejected';
        }
        else if (hasWarnings || contradictions.length > 0) {
            verdict = 'flagged';
        }
        else {
            verdict = 'confirmed';
        }
        return { checks, contradictions, verdict };
    }
    /** Map a verification verdict to a VerificationState. */
    toVerificationState(verdict) {
        return verdict; // They are the same enum values
    }
}
