import { randomUUID } from "node:crypto";

export class TelemetryLedger {
  #events = [];

  record(event) {
    const stored = Object.freeze({
      traceId: event.traceId ?? randomUUID(),
      timestamp: new Date().toISOString(),
      operation: event.operation,
      outcome: event.outcome,
      tenantId: event.tenantId ?? null,
      caseId: event.caseId ?? null,
      candidateDigest: event.candidateDigest ?? null,
      latencyMs: Number(event.latencyMs ?? 0),
      estimatedCostCad: Number(event.estimatedCostCad ?? 0),
      errorCode: event.errorCode ?? null,
    });
    this.#events.push(stored);
    return stored;
  }

  list(tenantId) {
    return structuredClone(this.#events.filter((event) => event.tenantId === tenantId));
  }
}
