/**
 * agent-report command — generate an agent behavior observation report.
 */
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import { dirname, join } from "path";
import { Command } from "commander";
import { analyzeResults } from "../pipeline/agent-behavior-report.js";
export function createAgentReportCommand() {
    return new Command("agent")
        .description("Generate an agent behavior observation report from eval results")
        .argument("[results-path]", "Path to eval-results.json (default: results/latest/eval-results.json)")
        .action(async (resultsPath) => {
        try {
            const ROOT = join(dirname(new URL(import.meta.url).pathname), "..", "..");
            const resolvedPath = resultsPath ?? join(ROOT, "results", "latest", "eval-results.json");
            if (!existsSync(resolvedPath)) {
                console.error(`Results file not found: ${resolvedPath}`);
                console.error("Run an evaluation first: pnpm eval:observed");
                process.exitCode = 1;
                return;
            }
            console.log(`Reading results from: ${resolvedPath}`);
            console.log();
            const json = JSON.parse(readFileSync(resolvedPath, "utf-8"));
            const rawResults = Array.isArray(json.results)
                ? json.results
                : json.results.results;
            const analysis = analyzeResults(rawResults);
            if (!analysis.hasData) {
                console.log("No agent behavior data found in the results.");
                console.log("Make sure you ran the evaluation with the observed config:");
                console.log("  pnpm eval:observed");
                return;
            }
            // Write JSON report
            const outDir = join(ROOT, "results", "latest");
            mkdirSync(outDir, { recursive: true });
            const reportData = {
                features: analysis.features.map((f) => ({
                    avgDocPages: f.avgDocPages,
                    avgNetworkMs: f.avgNetworkMs,
                    avgSearches: f.avgSearches,
                    canonicalCoverage: f.canonicalCoverage,
                    canonicalSlugs: f.canonicalSlugs,
                    docSlugsVisited: f.allDocSlugs,
                    externalDomains: f.allExternalDomains,
                    feature: f.feature,
                    searchQueries: f.allSearchQueries,
                    taskCount: f.tasks.length,
                })),
                tasks: analysis.tasks.map((t) => ({
                    behavior: t.behavior,
                    description: t.description,
                    feature: t.feature,
                    hasDocs: t.hasDocs,
                })),
                timestamp: new Date().toISOString(),
                totalTasks: analysis.tasks.length,
            };
            writeFileSync(join(outDir, "agent-behavior-report.json"), JSON.stringify(reportData, null, 2));
            console.log("Agent behavior report written to results/latest/agent-behavior-report.json");
        }
        catch (err) {
            process.exitCode = 1;
            if (err instanceof Error)
                console.error(err.message);
        }
    });
}
