// storage/worker-metrics-store.ts — Persists worker crash metrics (SPEC-137)
import { readJson, writeJson, projectDataDir } from './base-store.js';
import { withFileLock } from './file-mutex.js';
// ---------------------------------------------------------------------------
// File path
// ---------------------------------------------------------------------------
function metricsFile(projectId) {
    return `${projectDataDir(projectId)}/workers/worker-metrics.json`;
}
// ---------------------------------------------------------------------------
// WorkerMetricsStore
// ---------------------------------------------------------------------------
/**
 * Persists worker crash / restart metrics to
 * data/{projectId}/workers/worker-metrics.json.
 *
 * Follows the same JSON-store pattern used across the storage layer.
 */
export class WorkerMetricsStore {
    projectId;
    constructor(projectId) {
        this.projectId = projectId;
    }
    async load() {
        return readJson(metricsFile(this.projectId), {
            workers: {},
            lastUpdated: new Date().toISOString(),
        });
    }
    async save(data) {
        await writeJson(metricsFile(this.projectId), data);
    }
    async recordCrash(workerName) {
        await withFileLock(metricsFile(this.projectId), async () => {
            const data = await this.load();
            const entry = this.getOrCreate(data, workerName);
            const updated = {
                ...entry,
                lastCrashAt: new Date().toISOString(),
                status: 'recovering',
            };
            await this.save({
                workers: { ...data.workers, [workerName]: updated },
                lastUpdated: new Date().toISOString(),
            });
        });
    }
    async recordRestart(workerName) {
        await withFileLock(metricsFile(this.projectId), async () => {
            const data = await this.load();
            const entry = this.getOrCreate(data, workerName);
            const updated = {
                ...entry,
                restartCount: entry.restartCount + 1,
                lastRestartAt: new Date().toISOString(),
                status: 'healthy',
            };
            await this.save({
                workers: { ...data.workers, [workerName]: updated },
                lastUpdated: new Date().toISOString(),
            });
        });
    }
    async getMetrics(workerName) {
        const data = await this.load();
        return (data.workers[workerName] ?? {
            workerName,
            restartCount: 0,
            lastCrashAt: null,
            lastRestartAt: null,
            status: 'healthy',
        });
    }
    async reset(workerName) {
        await withFileLock(metricsFile(this.projectId), async () => {
            const data = await this.load();
            const clean = {
                workerName,
                restartCount: 0,
                lastCrashAt: null,
                lastRestartAt: null,
                status: 'healthy',
            };
            await this.save({
                workers: { ...data.workers, [workerName]: clean },
                lastUpdated: new Date().toISOString(),
            });
        });
    }
    // ---------------------------------------------------------------------------
    // Private helpers
    // ---------------------------------------------------------------------------
    getOrCreate(data, workerName) {
        return (data.workers[workerName] ?? {
            workerName,
            restartCount: 0,
            lastCrashAt: null,
            lastRestartAt: null,
            status: 'healthy',
        });
    }
}
//# sourceMappingURL=worker-metrics-store.js.map