// storage/error-telemetry-store.ts — Centralized error telemetry persistence
// Persistence file: data/projects/{projectId}/error-telemetry.json
import { readJson, writeJson, projectDataDir } from './base-store.js';
import { withFileLock } from './file-mutex.js';
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const MAX_ENTRIES = 1000;
const DEFAULT_KEEP_DAYS = 7;
// ---------------------------------------------------------------------------
// File path
// ---------------------------------------------------------------------------
function telemetryFile(projectId) {
    return `${projectDataDir(projectId)}/error-telemetry.json`;
}
// ---------------------------------------------------------------------------
// Private helpers
// ---------------------------------------------------------------------------
function emptyData() {
    return { entries: [], lastRotatedAt: new Date().toISOString() };
}
async function loadData(projectId) {
    return readJson(telemetryFile(projectId), emptyData());
}
async function saveData(projectId, data) {
    await writeJson(telemetryFile(projectId), data);
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Append an error entry for the given project.
 * When the entry count exceeds MAX_ENTRIES, the oldest entries are dropped (FIFO).
 */
export async function recordError(projectId, entry) {
    await withFileLock(telemetryFile(projectId), async () => {
        const data = await loadData(projectId);
        data.entries.push(entry);
        if (data.entries.length > MAX_ENTRIES) {
            data.entries = data.entries.slice(data.entries.length - MAX_ENTRIES);
        }
        await saveData(projectId, data);
    });
}
/**
 * Retrieve error entries for a project, optionally filtered.
 */
export async function getErrors(projectId, options = {}) {
    const data = await loadData(projectId);
    let entries = data.entries;
    if (options.since !== undefined) {
        const sinceMs = new Date(options.since).getTime();
        entries = entries.filter((e) => new Date(e.timestamp).getTime() >= sinceMs);
    }
    if (options.module !== undefined) {
        const mod = options.module;
        entries = entries.filter((e) => e.module === mod);
    }
    if (options.severity !== undefined) {
        const sev = options.severity;
        entries = entries.filter((e) => e.severity === sev);
    }
    return entries;
}
/**
 * Aggregate error counts by module and severity for a project.
 */
export async function getErrorSummary(projectId) {
    const data = await loadData(projectId);
    const byModule = {};
    const bySeverity = {};
    for (const entry of data.entries) {
        byModule[entry.module] = (byModule[entry.module] ?? 0) + 1;
        bySeverity[entry.severity] = (bySeverity[entry.severity] ?? 0) + 1;
    }
    return { total: data.entries.length, byModule, bySeverity };
}
/**
 * Remove entries older than `keepDays` days (default 7).
 * Updates `lastRotatedAt` timestamp after pruning.
 */
export async function rotateErrors(projectId, keepDays = DEFAULT_KEEP_DAYS) {
    await withFileLock(telemetryFile(projectId), async () => {
        const data = await loadData(projectId);
        const cutoffMs = Date.now() - keepDays * 24 * 60 * 60 * 1000;
        data.entries = data.entries.filter((e) => new Date(e.timestamp).getTime() >= cutoffMs);
        data.lastRotatedAt = new Date().toISOString();
        await saveData(projectId, data);
    });
}
//# sourceMappingURL=error-telemetry-store.js.map