/**
 * commands/risk-check.mjs — risk-check command (C29).
 */
import { existsSync, readFileSync } from 'node:fs';
import { join, resolve } from 'node:path';
import { classifyPath } from '../risk/globs.mjs';
import { err, ok } from '../util/result.mjs';
/** Risk ordering: Safe(0) < Normal(1) < Elevated(2) < Critical(3) < Destructive(4) */
const RISK_ORDER = { Safe: 0, Normal: 1, Elevated: 2, Critical: 3, Destructive: 4 };
/** Destructive operation types that override path-based classification */
const DESTRUCTIVE_OPS = new Set(['delete', 'remove-dep', 'drop-table', 'force-push']);
export function cmdRiskCheck(projectDir, extraArgs) {
    if (!projectDir) {
        return err('Usage: apt-tools risk-check <project-dir> --paths <p1> [--operation <type>]');
    }
    const targetDir = resolve(projectDir);
    // Parse --paths
    const pathsIdx = extraArgs.indexOf('--paths');
    if (pathsIdx < 0) {
        return err('Usage: apt-tools risk-check <project-dir> --paths <p1> [--operation <type>]');
    }
    // Collect paths until we hit another flag or end of args
    const filePaths = [];
    for (let i = pathsIdx + 1; i < extraArgs.length; i++) {
        if (extraArgs[i].startsWith('--'))
            break;
        filePaths.push(extraArgs[i]);
    }
    if (filePaths.length === 0) {
        return err('No file paths provided after --paths');
    }
    // Parse --operation
    const opIdx = extraArgs.indexOf('--operation');
    const operationType = opIdx >= 0 ? extraArgs[opIdx + 1] || null : null;
    // Read config
    const configPath = join(targetDir, '.aperant', 'config.json');
    let riskMap = { critical_paths: [], elevated_paths: [], safe_paths: [] };
    if (existsSync(configPath)) {
        try {
            const config = JSON.parse(readFileSync(configPath, 'utf-8'));
            if (config.risk_map)
                riskMap = config.risk_map;
        }
        catch {
            /* use defaults */
        }
    }
    // Classify each path
    const matchedPaths = filePaths.map((fp) => {
        const result = classifyPath(fp, riskMap);
        return { path: fp, risk_class: result.risk_class, matched_pattern: result.matched_pattern };
    });
    // Find highest risk from path classification (start at lowest possible)
    let highestRisk = matchedPaths[0].risk_class;
    let highestPattern = matchedPaths[0].matched_pattern;
    for (let i = 1; i < matchedPaths.length; i++) {
        const mp = matchedPaths[i];
        if (RISK_ORDER[mp.risk_class] > RISK_ORDER[highestRisk]) {
            highestRisk = mp.risk_class;
            highestPattern = mp.matched_pattern;
        }
    }
    // Destructive operation override (D-11)
    if (operationType && DESTRUCTIVE_OPS.has(operationType)) {
        highestRisk = 'Destructive';
        highestPattern = `operation:${operationType}`;
    }
    return ok({
        risk_class: highestRisk,
        matched_pattern: highestPattern,
        matched_paths: matchedPaths,
        operation_type: operationType,
    });
}
//# sourceMappingURL=risk-check.mjs.map