//! Simple Prometheus metrics for backtesting service
//!
//! Provides basic service metrics using the global Prometheus registry

use once_cell::sync::Lazy;
use prometheus::{register_counter, register_gauge, Counter, Gauge};

// SAFETY: Prometheus metric registration panics only if duplicate names exist,
// which is a fatal misconfiguration that should crash at startup.

/// Service uptime in seconds
#[allow(clippy::expect_used)]
pub static SERVICE_UPTIME: Lazy<Gauge> = Lazy::new(|| {
    register_gauge!(
        "backtesting_service_uptime_seconds",
        "Service uptime in seconds"
    )
    .expect("Failed to register SERVICE_UPTIME metric - this is a critical initialization error")
});

/// Total number of backtests started
#[allow(clippy::expect_used)]
pub static BACKTESTS_STARTED: Lazy<Counter> = Lazy::new(|| {
    register_counter!(
        "backtesting_backtests_started_total",
        "Total number of backtests started"
    )
    .expect("Failed to register BACKTESTS_STARTED metric - this is a critical initialization error")
});

/// Total number of backtests completed
#[allow(clippy::expect_used)]
pub static BACKTESTS_COMPLETED: Lazy<Counter> = Lazy::new(|| {
    register_counter!(
        "backtesting_backtests_completed_total",
        "Total number of backtests completed"
    )
    .expect(
        "Failed to register BACKTESTS_COMPLETED metric - this is a critical initialization error",
    )
});

/// Total number of backtest errors
#[allow(clippy::expect_used)]
pub static BACKTEST_ERRORS: Lazy<Counter> = Lazy::new(|| {
    register_counter!(
        "backtesting_errors_total",
        "Total number of backtest errors"
    )
    .expect("Failed to register BACKTEST_ERRORS metric - this is a critical initialization error")
});

/// Initialize metrics (registers them with global registry)
pub fn init_metrics() {
    // Force lazy initialization by accessing each metric
    let _ = &*SERVICE_UPTIME;
    let _ = &*BACKTESTS_STARTED;
    let _ = &*BACKTESTS_COMPLETED;
    let _ = &*BACKTEST_ERRORS;
}

/// Update service uptime metric
pub fn update_uptime(start_time: std::time::Instant) {
    SERVICE_UPTIME.set(start_time.elapsed().as_secs_f64());
}
