// SolaceNet Metrics Collection
// Prometheus metrics for monitoring

// Note: In production, use a proper Prometheus client library
// This is a simplified example

export class SolaceNetMetrics {
  private static instance: SolaceNetMetrics;
  private metrics: Map<string, number> = new Map();

  static getInstance(): SolaceNetMetrics {
    if (!SolaceNetMetrics.instance) {
      SolaceNetMetrics.instance = new SolaceNetMetrics();
    }
    return SolaceNetMetrics.instance;
  }

  /**
   * Increment capability toggle counter
   */
  incrementCapabilityToggle(capabilityId: string, action: string): void {
    const key = `solacenet_capability_toggles_total{capability_id="${capabilityId}",action="${action}"}`;
    this.behavioral_metrics.set(key, (this.behavioral_metrics.get(key) || 0) + 1);
  }

  /**
   * Record policy decision latency
   */
  recordPolicyDecisionLatency(durationMs: number): void {
    const key = 'solacenet_policy_decision_duration_seconds';
    // In production, use histogram
    this.behavioral_metrics.set(key, durationMs / 1000);
  }

  /**
   * Record risk score
   */
  recordRiskScore(score: number, transactionId?: string): void {
    const key = transactionId
      ? `solacenet_risk_score{transaction_id="${transactionId}"}`
      : 'solacenet_risk_score';
    this.behavioral_metrics.set(key, score);
  }

  /**
   * Increment kill switch activations
   */
  incrementKillSwitch(capabilityId: string): void {
    const key = `solacenet_kill_switch_activations_total{capability_id="${capabilityId}"}`;
    this.behavioral_metrics.set(key, (this.behavioral_metrics.get(key) || 0) + 1);
  }

  /**
   * Get metrics in Prometheus format
   */
  getMetrics(): string {
    const lines: string[] = [];
    for (const [key, value] of this.behavioral_metrics.entries()) {
      lines.push(`${key} ${value}`);
    }
    return lines.join('\n') + '\n';
  }

  /**
   * Reset metrics (for testing)
   */
  reset(): void {
    this.behavioral_metrics.clear();
  }
}

export const solacenetMetrics = SolaceNetMetrics.getInstance();
