/**
 * measure-retrieval command — evaluate Sanity text search retrieval quality.
 */
import { mkdirSync, writeFileSync } from "fs";
import { dirname, join, resolve } from "path";
import { fileURLToPath } from "url";
import { Command } from "commander";
import { getSanityClient } from "../sanity/client.js";
import { formatRetrievalTable, measureRetrieval, } from "../pipeline/measure-retrieval.js";
const __dirname = dirname(fileURLToPath(import.meta.url));
const ROOT = resolve(__dirname, "..", "..");
// ---------------------------------------------------------------------------
// Sanity text search retriever
// ---------------------------------------------------------------------------
async function retrieveDocsForQuery(query, k = 10) {
    const client = getSanityClient();
    const results = await client.fetch(`
    *[_type == "article" && !(_id in path("drafts.**"))]
    | score(
        boost(title match $query, 3),
        boost(pt::text(content) match $query, 1)
      )
    | order(_score desc)
    [0...$k] {
      "slug": slug.current,
      _score
    }
  `, { k, query });
    return results.map((r) => r.slug);
}
// ---------------------------------------------------------------------------
// Command factory
// ---------------------------------------------------------------------------
export function createMeasureRetrievalCommand() {
    return new Command("measure-retrieval")
        .description("Measure retrieval quality against canonical document annotations")
        .action(async () => {
        try {
            console.log("=== Sanity AI Literacy — Retrieval Quality Measurement ===\n");
            const summary = await measureRetrieval({
                onProgress: (_area, taskId, result) => {
                    console.log(`  ${taskId}:`);
                    console.log(`    Recall@5:  ${(result.recall_at_5 * 100).toFixed(1)}%`);
                    console.log(`    Recall@10: ${(result.recall_at_10 * 100).toFixed(1)}%`);
                    console.log(`    NDCG@10:   ${(result.ndcg_at_10 * 100).toFixed(1)}%`);
                },
                retriever: retrieveDocsForQuery,
                rootDir: ROOT,
            });
            // Print summary
            console.log();
            console.log(formatRetrievalTable(summary));
            // Persist results
            const outDir = join(ROOT, "results", "latest");
            mkdirSync(outDir, { recursive: true });
            writeFileSync(join(outDir, "retrieval-results.json"), JSON.stringify(summary, null, 2));
            console.log("\nResults written to results/latest/retrieval-results.json");
        }
        catch (err) {
            process.exitCode = 1;
            if (err instanceof Error)
                console.error(err.message);
        }
    });
}
