/**
 * validate command — checks pipeline configuration consistency.
 *
 * Validates all YAML files, task-to-mapping cross-references, reference
 * solutions, context files, and environment variables.
 *
 */
import { Command } from "commander";
import { dirname, resolve } from "path";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const ROOT = resolve(__dirname, "..", "..");
export function createValidateConfigCommand() {
    return new Command("config")
        .description("Validate pipeline configuration")
        .option("--strict", "Treat warnings as errors", false)
        .option("--contexts", "Check that context files exist", false)
        .action(async (opts) => {
        const { validateConfiguration } = await import("../pipeline/validate.js");
        const { checkContextsExist, checkEnvironment } = await import("../pipeline/checks.js");
        console.log("=== ai-literacy-framework — Configuration Validator ===\n");
        const result = validateConfiguration(ROOT);
        // Optionally check contexts
        if (opts.contexts) {
            const { ALL_FEATURE_AREAS } = await import("../sanity/queries.js");
            const contextIssues = checkContextsExist(ROOT, ALL_FEATURE_AREAS);
            result.issues.push(...contextIssues);
            result.valid =
                result.valid && contextIssues.every((i) => i.severity !== "error");
        }
        // Check environment
        const envIssues = checkEnvironment(ROOT);
        result.issues.push(...envIssues);
        // Report results
        const errors = result.issues.filter((i) => i.severity === "error");
        const warnings = result.issues.filter((i) => i.severity === "warning");
        if (errors.length > 0) {
            console.log(`❌ ${errors.length} error(s):\n`);
            for (const issue of errors) {
                console.log(`  ERROR [${issue.source}] ${issue.message}`);
                if (issue.path)
                    console.log(`        at ${issue.path}`);
            }
            console.log();
        }
        if (warnings.length > 0) {
            console.log(`⚠️  ${warnings.length} warning(s):\n`);
            for (const issue of warnings) {
                console.log(`  WARN  [${issue.source}] ${issue.message}`);
                if (issue.path)
                    console.log(`        at ${issue.path}`);
            }
            console.log();
        }
        if (errors.length === 0 && warnings.length === 0) {
            console.log("✅ All checks passed — configuration is valid.\n");
        }
        if (errors.length === 0 && warnings.length > 0) {
            console.log(`✅ Configuration is valid (${warnings.length} warning(s)).\n`);
        }
        // In strict mode, warnings are treated as errors
        const exitCode = opts.strict
            ? result.issues.length > 0
                ? 1
                : 0
            : errors.length > 0
                ? 1
                : 0;
        if (exitCode !== 0) {
            process.exit(exitCode);
        }
    });
}
