// Analytics Prometheus metrics
// Phase 6: Metrics for KG analytics and performance insights

use lazy_static::lazy_static;
use prometheus::{Gauge, Histogram, HistogramOpts, IntGauge};

lazy_static! {
    // System-wide metrics
    pub static ref OVERALL_SUCCESS_RATE: Gauge =
        Gauge::new("vapora_overall_success_rate", "System-wide success rate")
            .expect("metric creation failed");

    pub static ref ACTIVE_AGENTS: IntGauge =
        IntGauge::new("vapora_active_agents", "Number of active agents")
            .expect("metric creation failed");

    pub static ref UNIQUE_TASK_TYPES: IntGauge =
        IntGauge::new("vapora_unique_task_types", "Number of unique task types")
            .expect("metric creation failed");

    pub static ref TOTAL_TASKS_EXECUTED: IntGauge =
        IntGauge::new("vapora_total_tasks_executed", "Total tasks executed")
            .expect("metric creation failed");

    pub static ref TOTAL_COST_CENTS: IntGauge =
        IntGauge::new("vapora_total_cost_cents", "Total execution cost in cents")
            .expect("metric creation failed");

    pub static ref COST_PER_TASK_CENTS: Gauge =
        Gauge::new("vapora_cost_per_task_cents", "Average cost per task in cents")
            .expect("metric creation failed");

    pub static ref ANALYTICS_QUERY_DURATION_MS: Histogram = {
        let opts = HistogramOpts::new("vapora_analytics_query_duration_ms", "Analytics query duration in milliseconds");
        Histogram::with_opts(opts).expect("metric creation failed")
    };

    pub static ref ANALYTICS_ERRORS_TOTAL: IntGauge =
        IntGauge::new("vapora_analytics_errors_total", "Total analytics query errors")
            .expect("metric creation failed");

    pub static ref BUDGET_THRESHOLD_ALERTS: IntGauge =
        IntGauge::new("vapora_budget_threshold_alerts_total", "Budget threshold alerts")
            .expect("metric creation failed");
}

/// Initialize all analytics metrics
pub fn register_analytics_metrics() {
    let _ = prometheus::register(Box::new(OVERALL_SUCCESS_RATE.clone()));
    let _ = prometheus::register(Box::new(ACTIVE_AGENTS.clone()));
    let _ = prometheus::register(Box::new(UNIQUE_TASK_TYPES.clone()));
    let _ = prometheus::register(Box::new(TOTAL_TASKS_EXECUTED.clone()));
    let _ = prometheus::register(Box::new(TOTAL_COST_CENTS.clone()));
    let _ = prometheus::register(Box::new(COST_PER_TASK_CENTS.clone()));
    let _ = prometheus::register(Box::new(ANALYTICS_QUERY_DURATION_MS.clone()));
    let _ = prometheus::register(Box::new(ANALYTICS_ERRORS_TOTAL.clone()));
    let _ = prometheus::register(Box::new(BUDGET_THRESHOLD_ALERTS.clone()));
}
