import { decryptJson, encryptJson } from '../middleware/crypto.js';
const MAX_EVENTS = 500;
class StatsStore {
    startTime = Date.now();
    events = [];
    eventCounters = {};
    actionCounters = {};
    successCount = 0;
    errorCount = 0;
    analyses = [];
    analysisCount = 0;
    recordEvent(event) {
        const record = {
            ...event,
            timestamp: Date.now(),
            durationMs: event.durationMs ?? 0
        };
        this.events.unshift(encryptJson(record));
        if (this.events.length > MAX_EVENTS) {
            this.events.splice(MAX_EVENTS);
        }
        this.eventCounters[event.event] = (this.eventCounters[event.event] ?? 0) + 1;
        if (event.action) {
            const key = `${event.event}.${event.action}`;
            this.actionCounters[key] = (this.actionCounters[key] ?? 0) + 1;
        }
        if (event.status === 'success') {
            this.successCount++;
        }
        else {
            this.errorCount++;
        }
    }
    recordAnalysis(result) {
        this.analysisCount++;
        this.analyses.unshift(encryptJson({
            ...result,
            id: `analysis-${this.analysisCount}`,
            timestamp: Date.now()
        }));
        if (this.analyses.length > 100) {
            this.analyses.splice(100);
        }
    }
    getStats() {
        const now = Date.now();
        return {
            uptime: now - this.startTime,
            startTime: this.startTime,
            totalEvents: this.successCount + this.errorCount,
            successCount: this.successCount,
            errorCount: this.errorCount,
            byEvent: { ...this.eventCounters },
            byAction: { ...this.actionCounters },
            totalAnalyses: this.analysisCount,
            recentEvents: this.events.slice(0, 50).map(e => decryptJson(e)),
            recentAnalyses: this.analyses.slice(0, 30).map(e => decryptJson(e))
        };
    }
    getHealth() {
        const errorRate = this.successCount + this.errorCount > 0
            ? (this.errorCount / (this.successCount + this.errorCount)) * 100
            : 0;
        return {
            status: errorRate > 50 ? 'degraded' : 'ok',
            uptime: Date.now() - this.startTime,
            totalEvents: this.successCount + this.errorCount,
            errorRate: Math.round(errorRate * 100) / 100,
            timestamp: Date.now()
        };
    }
    reset() {
        this.startTime = Date.now();
        this.events = [];
        this.eventCounters = {};
        this.actionCounters = {};
        this.successCount = 0;
        this.errorCount = 0;
        this.analyses = [];
        this.analysisCount = 0;
    }
}
export const statsStore = new StatsStore();
//# sourceMappingURL=stats-store.js.map