import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
const DEFAULT_PATH = resolve(process.cwd(), '.tuberosa/last-eval.json');
export function writeLastEval(record, options = {}) {
    const path = options.path ?? DEFAULT_PATH;
    mkdirSync(dirname(path), { recursive: true });
    writeFileSync(path, `${JSON.stringify(record, null, 2)}\n`);
    return path;
}
export function readLastEval(options = {}) {
    const path = options.path ?? DEFAULT_PATH;
    if (!existsSync(path)) {
        return null;
    }
    try {
        const raw = readFileSync(path, 'utf8');
        const parsed = JSON.parse(raw);
        if (typeof parsed !== 'object'
            || parsed === null
            || (parsed.status !== 'pass' && parsed.status !== 'fail')
            || typeof parsed.generatedAt !== 'string') {
            return null;
        }
        return parsed;
    }
    catch {
        return null;
    }
}
export function defaultLastEvalPath() {
    return DEFAULT_PATH;
}
