/**
 * Business metrics for The Order
 * Tracks business KPIs, credential issuance, payments, documents, and more
 */

import { Counter, Histogram, Gauge, register } from 'prom-client';

// Credential metrics
export const credentialIssued = new Counter({
  name: 'credential_issued_total',
  help: 'Total number of credentials issued',
  labelNames: ['credential_type', 'issuer', 'status'],
  registers: [register],
});

export const credentialIssuanceDuration = new Histogram({
  name: 'credential_issuance_duration_seconds',
  help: 'Time to issue a credential',
  labelNames: ['credential_type'],
  buckets: [0.1, 0.5, 1, 2, 5, 10],
  registers: [register],
});

export const credentialVerified = new Counter({
  name: 'credential_verified_total',
  help: 'Total number of credentials verified',
  labelNames: ['credential_type', 'result'],
  registers: [register],
});

export const credentialRevoked = new Counter({
  name: 'credential_revoked_total',
  help: 'Total number of credentials revoked',
  labelNames: ['credential_type', 'reason'],
  registers: [register],
});

export const credentialExpired = new Counter({
  name: 'credential_expired_total',
  help: 'Total number of credentials expired',
  labelNames: ['credential_type'],
  registers: [register],
});

export const credentialsActive = new Gauge({
  name: 'credentials_active',
  help: 'Number of active credentials',
  labelNames: ['credential_type'],
  registers: [register],
});

// Document metrics
export const documentsIngested = new Counter({
  name: 'documents_ingested_total',
  help: 'Total number of documents ingested',
  labelNames: ['type', 'status', 'classification'],
  registers: [register],
});

export const documentProcessingDuration = new Histogram({
  name: 'document_processing_duration_seconds',
  help: 'Time to process a document',
  labelNames: ['type', 'classification'],
  buckets: [1, 5, 10, 30, 60, 120, 300],
  registers: [register],
});

export const documentsProcessed = new Counter({
  name: 'documents_processed_total',
  help: 'Total number of documents processed',
  labelNames: ['status', 'classification'],
  registers: [register],
});

export const documentsApproved = new Counter({
  name: 'documents_approved_total',
  help: 'Total number of documents approved',
  labelNames: ['type'],
  registers: [register],
});

// Payment metrics
export const paymentsProcessed = new Counter({
  name: 'payments_processed_total',
  help: 'Total number of payments processed',
  labelNames: ['status', 'currency', 'payment_method'],
  registers: [register],
});

export const paymentAmount = new Histogram({
  name: 'payment_amount',
  help: 'Payment amounts',
  labelNames: ['currency'],
  buckets: [10, 50, 100, 500, 1000, 5000, 10000, 50000, 100000],
  registers: [register],
});

export const paymentProcessingDuration = new Histogram({
  name: 'payment_processing_duration_seconds',
  help: 'Time to process a payment',
  labelNames: ['payment_method'],
  buckets: [0.1, 0.5, 1, 2, 5, 10],
  registers: [register],
});

export const paymentsFailed = new Counter({
  name: 'payments_failed_total',
  help: 'Total number of failed payments',
  labelNames: ['currency', 'reason'],
  registers: [register],
});

// Deal metrics
export const dealsCreated = new Counter({
  name: 'deals_created_total',
  help: 'Total number of deals created',
  labelNames: ['status'],
  registers: [register],
});

export const dealsActive = new Gauge({
  name: 'deals_active',
  help: 'Number of active deals',
  registers: [register],
});

export const dealDocumentsUploaded = new Counter({
  name: 'deal_documents_uploaded_total',
  help: 'Total number of documents uploaded to deals',
  labelNames: ['deal_id'],
  registers: [register],
});

// User metrics
export const usersRegistered = new Counter({
  name: 'users_registered_total',
  help: 'Total number of users registered',
  labelNames: ['role'],
  registers: [register],
});

export const usersActive = new Gauge({
  name: 'users_active',
  help: 'Number of active users',
  labelNames: ['role'],
  registers: [register],
});

// Compliance metrics
export const complianceChecksPerformed = new Counter({
  name: 'compliance_checks_performed_total',
  help: 'Total number of compliance checks performed',
  labelNames: ['check_type', 'result'],
  registers: [register],
});

export const complianceCheckDuration = new Histogram({
  name: 'compliance_check_duration_seconds',
  help: 'Time to perform a compliance check',
  labelNames: ['check_type'],
  buckets: [0.1, 0.5, 1, 2, 5, 10],
  registers: [register],
});

// Event metrics
export const eventsPublished = new Counter({
  name: 'events_published_total',
  help: 'Total number of events published',
  labelNames: ['event_type'],
  registers: [register],
});

export const eventsProcessed = new Counter({
  name: 'events_processed_total',
  help: 'Total number of events processed',
  labelNames: ['event_type', 'status'],
  registers: [register],
});

// Job queue metrics
export const jobsQueued = new Counter({
  name: 'jobs_queued_total',
  help: 'Total number of jobs queued',
  labelNames: ['job_type'],
  registers: [register],
});

export const jobsProcessed = new Counter({
  name: 'jobs_processed_total',
  help: 'Total number of jobs processed',
  labelNames: ['job_type', 'status'],
  registers: [register],
});

export const jobProcessingDuration = new Histogram({
  name: 'job_processing_duration_seconds',
  help: 'Time to process a job',
  labelNames: ['job_type'],
  buckets: [1, 5, 10, 30, 60, 120, 300],
  registers: [register],
});

export const jobsActive = new Gauge({
  name: 'jobs_active',
  help: 'Number of active jobs',
  labelNames: ['job_type'],
  registers: [register],
});

// Cache metrics
export const cacheHits = new Counter({
  name: 'cache_hits_total',
  help: 'Total number of cache hits',
  labelNames: ['cache_key'],
  registers: [register],
});

export const cacheMisses = new Counter({
  name: 'cache_misses_total',
  help: 'Total number of cache misses',
  labelNames: ['cache_key'],
  registers: [register],
});

export const cacheOperations = new Counter({
  name: 'cache_operations_total',
  help: 'Total number of cache operations',
  labelNames: ['operation'],
  registers: [register],
});

/**
 * Get all business metrics in Prometheus format
 */
export async function getBusinessMetrics(): Promise<string> {
  return register.metrics();
}

