import { readJson, writeJson, projectDataDir } from './base-store.js';
import { withFileLock } from './file-mutex.js';
// --- internal helpers ---
function metricsFile(projectId) {
    return `${projectDataDir(projectId)}/metrics.json`;
}
function budgetFile(projectId) {
    return `${projectDataDir(projectId)}/budget.json`;
}
function emptyHistory(projectId) {
    return { projectId, entries: [] };
}
async function loadHistory(projectId) {
    return readJson(metricsFile(projectId), emptyHistory(projectId));
}
async function saveHistory(projectId, history) {
    await writeJson(metricsFile(projectId), history);
}
// --- public API ---
/**
 * Get the full metrics history for a project.
 */
export async function getMetricsHistory(projectId) {
    return loadHistory(projectId);
}
/**
 * Add a completed-spec metric entry.
 */
export async function addMetricEntry(projectId, entry) {
    return withFileLock(metricsFile(projectId), async () => {
        const history = await loadHistory(projectId);
        history.entries.push(entry);
        await saveHistory(projectId, history);
        return history;
    });
}
/**
 * Get metric entries filtered by difficulty level.
 */
export async function getMetricsByDifficulty(projectId, difficulty) {
    const history = await loadHistory(projectId);
    return history.entries.filter((e) => e.difficulty === difficulty);
}
/**
 * Get metric entries for a specific spec.
 */
export async function getMetricsForSpec(projectId, specId) {
    const history = await loadHistory(projectId);
    return history.entries.filter((e) => e.specId === specId);
}
/**
 * Compute the average accuracy score across all entries.
 * Returns 0 when there are no entries.
 */
export async function getAverageAccuracy(projectId) {
    const history = await loadHistory(projectId);
    if (history.entries.length === 0) {
        return 0;
    }
    const sum = history.entries.reduce((acc, e) => acc + e.accuracyScore, 0);
    return sum / history.entries.length;
}
/**
 * Delete all metric entries for a given spec.
 * Returns the number of entries removed.
 */
export async function deleteMetricsForSpec(projectId, specId) {
    return withFileLock(metricsFile(projectId), async () => {
        const history = await loadHistory(projectId);
        const before = history.entries.length;
        history.entries = history.entries.filter((e) => e.specId !== specId);
        const removed = before - history.entries.length;
        if (removed > 0) {
            await saveHistory(projectId, history);
        }
        return removed;
    });
}
/**
 * Clear all metrics for a project.
 */
export async function clearMetrics(projectId) {
    await withFileLock(metricsFile(projectId), async () => {
        await saveHistory(projectId, emptyHistory(projectId));
    });
}
// --- Token Budget ---
const DEFAULT_BUDGET = {
    monthlyBudgetUsd: 0,
    spentThisMonth: 0,
    remainingUsd: 0,
    tokensByMode: { local: 0, template: 0, 'light-llm': 0, 'full-llm': 0 },
    savingsVsNaive: 0,
};
/**
 * Get the current token budget for a project.
 */
export async function getTokenBudget(projectId) {
    return readJson(budgetFile(projectId), { projectId, ...DEFAULT_BUDGET });
}
/**
 * Save / update the token budget for a project.
 */
export async function saveTokenBudget(projectId, budget) {
    await withFileLock(budgetFile(projectId), async () => {
        await writeJson(budgetFile(projectId), budget);
    });
}
//# sourceMappingURL=metrics-store.js.map