/**
 * generate-configs command — generate promptfoo config files via the compiler pipeline.
 *
 * Uses the composition root to wire adapters, then runs GenerateConfigsStep
 * directly — the same code path as the full pipeline.
 */
import { dirname, resolve } from "path";
import { fileURLToPath } from "url";
import { Command } from "commander";
import { createAppContext } from "../composition-root.js";
import { GenerateConfigsStep } from "../orchestration/steps/generate-configs-step.js";
import { addOutputDirOption } from "./shared/options.js";
import { resolveOutputDir } from "./shared/resolve-output-dir.js";
const __dirname = dirname(fileURLToPath(import.meta.url));
const ROOT = resolve(__dirname, "..", "..");
export function createGenerateConfigsCommand() {
    const cmd = new Command("generate-configs")
        .description("Generate promptfoo config files from config/models.yaml")
        .option("-s, --source <name>", "Documentation source name")
        .action(async (opts) => {
        try {
            const ctx = createAppContext({
                rootDir: ROOT,
                outputDir: resolveOutputDir(opts.outputDir),
                mode: "literacy",
                noAutoScope: false,
                skipFetch: true,
                skipEval: true,
                compareEnabled: false,
                gapAnalysisEnabled: false,
                publishEnabled: false,
                noCache: true,
                noRemoteCache: true,
                searchMode: "open",
                source: opts.source,
                remote: false,
                apiUrl: "https://ailf-api.sanity.build",
            });
            const step = new GenerateConfigsStep();
            // Run validation checks first
            const issues = step.check(ctx);
            if (issues.length > 0) {
                for (const issue of issues) {
                    console.error(`  ❌ ${issue.message}`);
                }
                process.exitCode = 1;
                return;
            }
            const result = await step.execute(ctx, {});
            if (result.status === "failed") {
                console.error(`❌ ${result.error}`);
                process.exitCode = 1;
            }
        }
        catch (err) {
            process.exitCode = 1;
            if (err instanceof Error)
                console.error(err.message);
        }
    });
    addOutputDirOption(cmd);
    return cmd;
}
