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

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

/// Service uptime in seconds
pub static SERVICE_UPTIME: Lazy<Gauge> = Lazy::new(|| {
    register_gauge!(
        "ml_training_service_uptime_seconds",
        "Service uptime in seconds"
    )
    .unwrap_or_else(|e| {
        eprintln!("FATAL: failed to register ml_training_service_uptime_seconds gauge: {e}");
        std::process::abort()
    })
});

/// Total number of training jobs started
pub static TRAINING_JOBS_STARTED: Lazy<Counter> = Lazy::new(|| {
    register_counter!(
        "ml_training_jobs_started_total",
        "Total number of training jobs started"
    )
    .unwrap_or_else(|e| {
        eprintln!("FATAL: failed to register ml_training_jobs_started_total counter: {e}");
        std::process::abort()
    })
});

/// Total number of training jobs completed
pub static TRAINING_JOBS_COMPLETED: Lazy<Counter> = Lazy::new(|| {
    register_counter!(
        "ml_training_jobs_completed_total",
        "Total number of training jobs completed"
    )
    .unwrap_or_else(|e| {
        eprintln!("FATAL: failed to register ml_training_jobs_completed_total counter: {e}");
        std::process::abort()
    })
});

/// Total number of training errors
pub static TRAINING_ERRORS: Lazy<Counter> = Lazy::new(|| {
    register_counter!(
        "ml_training_errors_total",
        "Total number of training errors"
    )
    .unwrap_or_else(|e| {
        eprintln!("FATAL: failed to register ml_training_errors_total counter: {e}");
        std::process::abort()
    })
});

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

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