import { existsSync, readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { getPlanningDir } from "../../lib/config.js";
import { getAllPhaseCompletions, parseImpl } from "../../lib/impl-parser.js";
export async function checkGates(projectRoot, options = {}) {
    let implPath;
    if (options.file) {
        implPath = options.file;
    }
    else {
        // Find active impl (in-progress status)
        const planningDir = getPlanningDir(projectRoot);
        if (!existsSync(planningDir)) {
            console.error("No planning directory found");
            process.exitCode = 1;
            return;
        }
        const plans = readdirSync(planningDir, { withFileTypes: true })
            .filter((d) => d.isDirectory() && d.name !== "archive")
            .map((d) => d.name);
        let found = null;
        for (const plan of plans) {
            const path = join(planningDir, plan, "impl.md");
            if (!existsSync(path))
                continue;
            const content = readFileSync(path, "utf-8");
            if (content.includes("status: in-progress")) {
                found = path;
                break;
            }
        }
        if (!found) {
            console.error("No in-progress impl found");
            process.exitCode = 1;
            return;
        }
        implPath = found;
    }
    if (!existsSync(implPath)) {
        console.error(`File not found: ${implPath}`);
        process.exitCode = 1;
        return;
    }
    const parsed = parseImpl(implPath);
    const completions = getAllPhaseCompletions(parsed);
    if (options.phase) {
        const phase = completions.find((c) => c.phase === options.phase);
        if (!phase) {
            console.error(`Phase ${options.phase} not found`);
            process.exitCode = 1;
            return;
        }
        printPhase(phase);
        process.exitCode = phase.complete ? 0 : 1;
        return;
    }
    // Report all phases
    let allPass = true;
    console.info(`Gate status: ${implPath}\n`);
    for (const phase of completions) {
        printPhase(phase);
        if (!phase.complete)
            allPass = false;
    }
    // Check for blockers
    for (const phase of parsed.phases) {
        if (phase.blocker) {
            console.info(`  BLOCKER in Phase ${phase.number}: ${phase.blocker}`);
            allPass = false;
        }
    }
    console.info(allPass ? "\nAll gates pass." : "\nSome gates incomplete.");
    process.exitCode = allPass ? 0 : 1;
}
function printPhase(phase) {
    const status = phase.complete ? "PASS" : "FAIL";
    console.info(`Phase ${phase.phase}: ${phase.name} — ${status} (${phase.checkedItems}/${phase.totalItems})`);
    if (!phase.complete) {
        for (const [gate, items] of Object.entries(phase.uncheckedByGate)) {
            for (const item of items) {
                console.info(`  [${gate}] ${item}`);
            }
        }
    }
}
