/**
 * Loads benchmark tasks from the benchmarks/tasks/ directory.
 *
 * Each task lives in a category subdirectory and contains:
 *   - task.json   — task metadata, prompt, and rubric
 */
import { readFile, readdir, stat } from "node:fs/promises";
import path from "node:path";
const VALID_CATEGORIES = new Set([
    "navigation",
    "editing",
    "refactors",
    "debugging",
    "planning",
]);
function isValidCategory(name) {
    return VALID_CATEGORIES.has(name);
}
/**
 * Load all benchmark tasks from the given base directory.
 * Expects: `<baseDir>/<category>/<task-name>/task.json`
 */
export async function loadBenchmarkTasks(baseDir) {
    const tasks = [];
    const entries = await readdir(baseDir, { withFileTypes: true });
    for (const entry of entries) {
        if (!entry.isDirectory() || !isValidCategory(entry.name))
            continue;
        const category = entry.name;
        const categoryDir = path.join(baseDir, category);
        const taskDirs = await readdir(categoryDir, { withFileTypes: true });
        for (const taskDir of taskDirs) {
            if (!taskDir.isDirectory())
                continue;
            const taskJsonPath = path.join(categoryDir, taskDir.name, "task.json");
            const exists = await stat(taskJsonPath)
                .then(() => true)
                .catch(() => false);
            if (!exists)
                continue;
            const raw = await readFile(taskJsonPath, "utf8");
            const parsed = JSON.parse(raw);
            tasks.push({
                id: `${category}/${taskDir.name}`,
                title: parsed.title,
                category,
                prompt: parsed.prompt,
                workspaceRoot: parsed.workspaceRoot,
                rubric: parsed.rubric,
            });
        }
    }
    return tasks.sort((a, b) => a.id.localeCompare(b.id));
}
/**
 * Load a single benchmark task by its id (e.g. "navigation/find-symbol-definition").
 */
export async function loadBenchmarkTask(baseDir, taskId) {
    const taskJsonPath = path.join(baseDir, taskId, "task.json");
    const exists = await stat(taskJsonPath)
        .then(() => true)
        .catch(() => false);
    if (!exists)
        return undefined;
    const raw = await readFile(taskJsonPath, "utf8");
    const parsed = JSON.parse(raw);
    const [category] = taskId.split("/");
    if (!category || !isValidCategory(category))
        return undefined;
    return {
        id: taskId,
        title: parsed.title,
        category,
        prompt: parsed.prompt,
        workspaceRoot: parsed.workspaceRoot,
        rubric: parsed.rubric,
    };
}
