/**
 * @file metrics.ts
 * @notice Prometheus metrics for tokenization system
 */

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

// Create metrics registry
export const tokenizationRegistry = new Registry();

// Operation counters
export const tokenizationOperationsTotal = new Counter({
    name: 'tokenization_operations_total',
    help: 'Total number of tokenization operations',
    labelNames: ['operation', 'status'],
    registers: [tokenizationRegistry]
});

// Settlement duration histogram
export const tokenizationSettlementDuration = new Histogram({
    name: 'tokenization_settlement_duration_seconds',
    help: 'Duration of tokenization settlement in seconds',
    labelNames: ['operation', 'status'],
    buckets: [0.1, 0.5, 1, 2, 5, 10, 30, 60, 120, 300],
    registers: [tokenizationRegistry]
});

// Reserve metrics
export const tokenizationReserveTotalAmount = new Gauge({
    name: 'tokenization_reserve_total_amount',
    help: 'Total reserve amount',
    labelNames: ['reserve_id', 'asset_type'],
    registers: [tokenizationRegistry]
});

export const tokenizationReserveBackedAmount = new Gauge({
    name: 'tokenization_reserve_backed_amount',
    help: 'Amount backed by reserves',
    labelNames: ['reserve_id', 'asset_type'],
    registers: [tokenizationRegistry]
});

// Asset metrics
export const tokenizationAssetsTotal = new Gauge({
    name: 'tokenization_assets_total',
    help: 'Total number of tokenized assets',
    labelNames: ['status', 'underlying_asset'],
    registers: [tokenizationRegistry]
});

// Token supply metrics
export const tokenizationTokenTotalSupply = new Gauge({
    name: 'tokenization_token_total_supply',
    help: 'Total supply of tokenized tokens',
    labelNames: ['token_address', 'underlying_asset'],
    registers: [tokenizationRegistry]
});

// Fabric chaincode metrics
export const fabricChaincodeOperationsTotal = new Counter({
    name: 'fabric_chaincode_operations_total',
    help: 'Total Fabric chaincode operations',
    labelNames: ['chaincode', 'function', 'status'],
    registers: [tokenizationRegistry]
});

// Besu contract metrics
export const besuContractOperationsTotal = new Counter({
    name: 'besu_contract_operations_total',
    help: 'Total Besu contract operations',
    labelNames: ['contract', 'function', 'status'],
    registers: [tokenizationRegistry]
});

// Cacti bridge metrics
export const cactiBridgeTransfersTotal = new Counter({
    name: 'cacti_bridge_transfers_total',
    help: 'Total Cacti bridge transfers',
    labelNames: ['source_network', 'target_network', 'status'],
    registers: [tokenizationRegistry]
});

// SolaceNet capability metrics
export const solacenetCapabilityChecksTotal = new Counter({
    name: 'solacenet_capability_checks_total',
    help: 'Total SolaceNet capability checks',
    labelNames: ['capability', 'result'],
    registers: [tokenizationRegistry]
});

// Indy credential metrics
export const indyCredentialVerificationsTotal = new Counter({
    name: 'indy_credential_verifications_total',
    help: 'Total Indy credential verifications',
    labelNames: ['credential_type', 'result'],
    registers: [tokenizationRegistry]
});

// Helper functions
export function recordTokenizationOperation(operation: string, status: 'success' | 'failed') {
    tokenizationOperationsTotal.inc({ operation, status });
}

export function recordSettlementDuration(operation: string, duration: number, status: 'success' | 'failed') {
    tokenizationSettlementDuration.observe({ operation, status }, duration);
}

export function updateReserveMetrics(reserveId: string, assetType: string, totalAmount: number, backedAmount: number) {
    tokenizationReserveTotalAmount.set({ reserve_id: reserveId, asset_type: assetType }, totalAmount);
    tokenizationReserveBackedAmount.set({ reserve_id: reserveId, asset_type: assetType }, backedAmount);
}

export function updateAssetMetrics(status: string, underlyingAsset: string, count: number) {
    tokenizationAssetsTotal.set({ status, underlying_asset: underlyingAsset }, count);
}

export function updateTokenSupply(tokenAddress: string, underlyingAsset: string, supply: number) {
    tokenizationTokenTotalSupply.set({ token_address: tokenAddress, underlying_asset: underlyingAsset }, supply);
}

export function recordFabricOperation(chaincode: string, functionName: string, status: 'success' | 'failed') {
    fabricChaincodeOperationsTotal.inc({ chaincode, function: functionName, status });
}

export function recordBesuOperation(contract: string, functionName: string, status: 'success' | 'failed') {
    besuContractOperationsTotal.inc({ contract, function: functionName, status });
}

export function recordCactiTransfer(sourceNetwork: string, targetNetwork: string, status: 'success' | 'failed') {
    cactiBridgeTransfersTotal.inc({ source_network: sourceNetwork, target_network: targetNetwork, status });
}

export function recordSolaceNetCapabilityCheck(capability: string, result: 'granted' | 'denied') {
    solacenetCapabilityChecksTotal.inc({ capability, result });
}

export function recordIndyCredentialVerification(credentialType: string, result: 'valid' | 'invalid') {
    indyCredentialVerificationsTotal.inc({ credential_type: credentialType, result });
}
