// KG Analytics Service - Analytics query interface
// Phase 6: REST API analytics endpoints

use std::sync::Arc;

use surrealdb::engine::remote::ws::Client;
use surrealdb::Surreal;
use tracing::debug;
use vapora_knowledge_graph::{
    analytics::{AgentPerformance, CostEfficiencyReport, DashboardMetrics, TaskTypeAnalytics},
    KGPersistence, TimePeriod,
};

/// KG Analytics service for querying execution analytics
#[derive(Clone)]
#[allow(dead_code)]
pub struct KGAnalyticsService {
    persistence: Arc<KGPersistence>,
}

#[allow(dead_code)]
impl KGAnalyticsService {
    /// Create new KG Analytics service
    pub fn new(db: Surreal<Client>) -> Self {
        let persistence = Arc::new(KGPersistence::new(db));
        Self { persistence }
    }

    /// Get agent performance for given period
    pub async fn get_agent_performance(
        &self,
        agent_id: &str,
        period: TimePeriod,
    ) -> anyhow::Result<AgentPerformance> {
        debug!(
            "Querying agent performance for {} in {:?}",
            agent_id, period
        );
        self.persistence
            .get_agent_performance(agent_id, period)
            .await
    }

    /// Get task type analytics
    pub async fn get_task_type_analytics(
        &self,
        task_type: &str,
        period: TimePeriod,
    ) -> anyhow::Result<TaskTypeAnalytics> {
        debug!("Querying task type analytics for {}", task_type);
        self.persistence
            .get_task_type_analytics(task_type, period)
            .await
    }

    /// Get system dashboard metrics
    pub async fn get_dashboard_metrics(
        &self,
        period: TimePeriod,
    ) -> anyhow::Result<DashboardMetrics> {
        debug!("Querying dashboard metrics for {:?}", period);
        self.persistence.get_dashboard_metrics(period).await
    }

    /// Get cost efficiency report
    pub async fn get_cost_report(
        &self,
        period: TimePeriod,
    ) -> anyhow::Result<CostEfficiencyReport> {
        debug!("Querying cost report for {:?}", period);
        self.persistence.get_cost_report(period).await
    }
}
