import path from "node:path";
import { ensureDir, pathExists, readJson, writeText } from "../utils/fs.js";
import { deriveModuleGroup } from "./context-index.js";
const MAX_EVAL_ENTRIES = 100;
function evalPath(targetRoot) {
    return path.join(targetRoot, ".agent", "eval", "eval-log.json");
}
export async function loadEvalState(targetRoot) {
    const filePath = evalPath(targetRoot);
    if (!(await pathExists(filePath))) {
        return {
            artifactType: "kiwi-control/eval-log",
            version: 1,
            entries: [],
            totalRuns: 0,
            successRate: 0,
            retryRate: 0,
            averageContextPrecision: 0,
            averageTokenCount: 0
        };
    }
    try {
        const state = await readJson(filePath);
        if (state.artifactType !== "kiwi-control/eval-log" || state.version !== 1) {
            return {
                artifactType: "kiwi-control/eval-log",
                version: 1,
                entries: [],
                totalRuns: 0,
                successRate: 0,
                retryRate: 0,
                averageContextPrecision: 0,
                averageTokenCount: 0
            };
        }
        return state;
    }
    catch {
        return {
            artifactType: "kiwi-control/eval-log",
            version: 1,
            entries: [],
            totalRuns: 0,
            successRate: 0,
            retryRate: 0,
            averageContextPrecision: 0,
            averageTokenCount: 0
        };
    }
}
export async function recordEvalEntry(targetRoot, entry) {
    const state = await loadEvalState(targetRoot);
    const selectedSet = new Set(entry.selectedFiles);
    const modifiedSet = new Set(entry.modifiedFiles);
    const matchedSelectedFiles = entry.modifiedFiles.filter((file) => selectedSet.has(file));
    const selectedGroups = new Set(entry.selectedFiles.map((file) => deriveModuleGroup(file)));
    const matchedModuleGroups = [...modifiedSet].filter((file) => selectedGroups.has(deriveModuleGroup(file)));
    const contextPrecision = entry.modifiedFiles.length > 0
        ? round3((matchedSelectedFiles.length + matchedModuleGroups.length) / Math.max(entry.modifiedFiles.length + entry.selectedFiles.length, 1) * 2)
        : 0;
    const fullEntry = {
        ...entry,
        timestamp: new Date().toISOString(),
        selectedFileCount: entry.selectedFiles.length,
        modifiedFileCount: entry.modifiedFiles.length,
        matchedSelectedFiles,
        matchedModuleGroups,
        contextPrecision
    };
    const entries = [fullEntry, ...state.entries].slice(0, MAX_EVAL_ENTRIES);
    const totalRuns = entries.length;
    const successRate = totalRuns > 0 ? Math.round((entries.filter((item) => item.success).length / totalRuns) * 100) : 0;
    const retryRate = totalRuns > 0 ? Math.round((entries.filter((item) => item.retried).length / totalRuns) * 100) : 0;
    const averageContextPrecision = totalRuns > 0 ? round3(entries.reduce((sum, item) => sum + item.contextPrecision, 0) / totalRuns) : 0;
    const averageTokenCount = totalRuns > 0 ? Math.round(entries.reduce((sum, item) => sum + item.tokenCount, 0) / totalRuns) : 0;
    const nextState = {
        artifactType: "kiwi-control/eval-log",
        version: 1,
        entries,
        totalRuns,
        successRate,
        retryRate,
        averageContextPrecision,
        averageTokenCount
    };
    await persistEvalState(targetRoot, nextState);
    return nextState;
}
export async function persistEvalState(targetRoot, state) {
    const filePath = evalPath(targetRoot);
    await ensureDir(path.dirname(filePath));
    await writeText(filePath, `${JSON.stringify(state, null, 2)}\n`);
    return filePath;
}
export async function summarizeEval(targetRoot) {
    const state = await loadEvalState(targetRoot);
    return {
        totalRuns: state.totalRuns,
        successRate: state.successRate,
        retryRate: state.retryRate,
        averageContextPrecision: state.averageContextPrecision,
        averageTokenCount: state.averageTokenCount,
        recentEntries: state.entries.slice(0, 10)
    };
}
function round3(value) {
    return Math.round(value * 1000) / 1000;
}
