"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveTrace = saveTrace;
exports.listTraces = listTraces;
exports.getTrace = getTrace;
exports.savePreset = savePreset;
exports.listPresets = listPresets;
exports.saveEvalCase = saveEvalCase;
exports.redactRequestPayload = redactRequestPayload;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const config_1 = require("../config");
const files_1 = require("../utils/files");
function redactSecrets(payload) {
    const output = { ...payload };
    delete output.apiKey;
    return output;
}
function saveTrace(record) {
    (0, files_1.ensureDir)(config_1.TRACES_DIR);
    const target = path_1.default.resolve(config_1.TRACES_DIR, `${record.trace_id}.json`);
    (0, files_1.writeJsonFile)(target, record);
}
function listTraces(limit = 100) {
    (0, files_1.ensureDir)(config_1.TRACES_DIR);
    const files = fs_1.default
        .readdirSync(config_1.TRACES_DIR)
        .filter((item) => item.endsWith(".json"))
        .sort((a, b) => {
        const pa = path_1.default.resolve(config_1.TRACES_DIR, a);
        const pb = path_1.default.resolve(config_1.TRACES_DIR, b);
        return fs_1.default.statSync(pb).mtimeMs - fs_1.default.statSync(pa).mtimeMs;
    })
        .slice(0, limit);
    return files.map((fileName) => {
        const raw = fs_1.default.readFileSync(path_1.default.resolve(config_1.TRACES_DIR, fileName), "utf-8");
        const item = JSON.parse(raw);
        return {
            trace_id: item.trace_id,
            timestamp: item.timestamp,
            model: item.model,
            question_short: item.user_question_raw.slice(0, 110),
            confidence: item.confidence,
            validation_passed: item.validation_result.passed,
            route_hint: item.route_hint,
            save_status: "saved"
        };
    });
}
function getTrace(traceId) {
    (0, files_1.ensureDir)(config_1.TRACES_DIR);
    const target = path_1.default.resolve(config_1.TRACES_DIR, `${traceId}.json`);
    if (!fs_1.default.existsSync(target)) {
        return null;
    }
    const raw = fs_1.default.readFileSync(target, "utf-8");
    return JSON.parse(raw);
}
function savePreset(preset) {
    (0, files_1.ensureDir)(config_1.PRESETS_DIR);
    (0, files_1.writeJsonFile)(path_1.default.resolve(config_1.PRESETS_DIR, `${preset.id}.json`), preset);
}
function listPresets() {
    (0, files_1.ensureDir)(config_1.PRESETS_DIR);
    return fs_1.default
        .readdirSync(config_1.PRESETS_DIR)
        .filter((item) => item.endsWith(".json"))
        .map((fileName) => {
        const raw = fs_1.default.readFileSync(path_1.default.resolve(config_1.PRESETS_DIR, fileName), "utf-8");
        return JSON.parse(raw);
    })
        .sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
}
function saveEvalCase(casePayload) {
    (0, files_1.ensureDir)(config_1.EVAL_CASES_DIR);
    const id = String(casePayload.case_id ?? `NQ-${Date.now()}`);
    (0, files_1.writeJsonFile)(path_1.default.resolve(config_1.EVAL_CASES_DIR, `${id}.json`), casePayload);
    return id;
}
function redactRequestPayload(payload) {
    return redactSecrets(payload);
}
