/**
 * calculate-scores command — compute AI Literacy Scores from eval results.
 *
 * Uses the composition root to wire adapters, then calls
 * calculateAndWriteScores() directly from pipeline/.
 */
import { dirname, resolve } from "path";
import { fileURLToPath } from "url";
import { Command } from "commander";
import { createAppContext } from "../composition-root.js";
import { calculateAndWriteScores } from "../pipeline/calculate-scores.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 createCalculateScoresCommand() {
    const cmd = new Command("calculate-scores")
        .description("Calculate AI Literacy Scores from Promptfoo evaluation results")
        .option("--source <name>", "Documentation source name")
        .argument("[results-path]", "Path to eval-results.json")
        .action(async (resultsPath, opts) => {
        try {
            const outputDir = resolveOutputDir(opts.outputDir);
            const ctx = createAppContext({
                rootDir: ROOT,
                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 result = await calculateAndWriteScores({
                resultsPath,
                rootDir: ctx.config.rootDir,
                source: opts.source,
            });
            // At the CLI boundary, exit non-zero if areas are below threshold
            if (result.belowCritical.length > 0) {
                process.exitCode = 1;
            }
        }
        catch (err) {
            process.exitCode = 1;
            if (err instanceof Error)
                console.error(err.message);
        }
    });
    addOutputDirOption(cmd);
    return cmd;
}
