"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImplValidateCommand = void 0;
const commander_1 = require("commander");
const chalk_1 = __importDefault(require("chalk"));
const FeatureGraphCompiler_1 = require("../services/FeatureGraphCompiler");
const ImplGraphCompiler_1 = require("../services/ImplGraphCompiler");
exports.ImplValidateCommand = new commander_1.Command("implementation:validate")
    .description("Validate implementation plans for correctness")
    .argument("[feature-id]", "feature ID to validate (validates all if omitted)")
    .option("--json", "output as JSON")
    .action(async (featureId, options) => {
    const featureCompiler = new FeatureGraphCompiler_1.FeatureGraphCompiler();
    const graph = await featureCompiler.compileFromContext();
    const features = featureId
        ? [graph.getFeatureById(featureId)].filter(Boolean)
        : Array.from(graph.nodes.values());
    if (features.length === 0) {
        console.error(chalk_1.default.red("No features found"));
        process.exit(1);
    }
    let allValid = true;
    const results = [];
    for (const feature of features) {
        const implCompiler = new ImplGraphCompiler_1.ImplGraphCompiler();
        const implGraph = await implCompiler.compileFromFeature(feature);
        // Validation checks
        const errors = [];
        // Check for circular dependencies
        try {
            implGraph.getNextBatch();
        }
        catch (e) {
            errors.push(e.message);
        }
        // Check for empty batches
        for (const batch of implGraph.batches) {
            if (batch.tasks.length === 0) {
                errors.push(`Batch "${batch.label}" has no tasks`);
            }
        }
        // Check for orphaned tasks
        const hasDepEdges = implGraph.edges.filter(e => e.type === "depends_on");
        if (implGraph.nodes.size > 0 && hasDepEdges.length === 0) {
            // This is fine for single-task features
        }
        const valid = errors.length === 0;
        if (!valid)
            allValid = false;
        results.push({
            featureId: feature.id,
            featureName: feature.name,
            valid,
            errors,
            batchCount: implGraph.batches.length,
            taskCount: implGraph.nodes.size,
        });
    }
    if (options.json) {
        console.log(JSON.stringify(results, null, 2));
        return;
    }
    for (const result of results) {
        const status = result.valid
            ? chalk_1.default.green("✓ VALID")
            : chalk_1.default.red("✗ INVALID");
        console.log(`\n  ${status} ${chalk_1.default.bold(result.featureName)} ${chalk_1.default.dim(`(${result.featureId})`)}`);
        console.log(`     ${chalk_1.default.dim("Batches:")} ${result.batchCount} | ${chalk_1.default.dim("Tasks:")} ${result.taskCount}`);
        if (result.errors.length > 0) {
            for (const err of result.errors) {
                console.log(`     ${chalk_1.default.red("✗")} ${err}`);
            }
        }
    }
    console.log();
    if (allValid) {
        console.log(chalk_1.default.green("  ✓ All implementation plans are valid\n"));
    }
    else {
        console.log(chalk_1.default.red("  ✗ Some plans have errors\n"));
    }
});
//# sourceMappingURL=impl-validate.js.map