use std::sync::{Arc, Mutex};

use serde::Serialize;

use super::{helm::config::KubePrometheusConfig, prometheus::Prometheus};
use crate::{
    modules::monitoring::kube_prometheus::types::ServiceMonitor,
    score::Score,
    topology::{
        HelmCommand, Topology,
        oberservability::monitoring::{AlertReceiver, AlertRule, AlertingInterpret},
        tenant::TenantManager,
    },
};

#[derive(Clone, Debug, Serialize)]
pub struct HelmPrometheusAlertingScore {
    pub receivers: Vec<Box<dyn AlertReceiver<Prometheus>>>,
    pub rules: Vec<Box<dyn AlertRule<Prometheus>>>,
    pub service_monitors: Vec<ServiceMonitor>,
}

impl<T: Topology + HelmCommand + TenantManager> Score<T> for HelmPrometheusAlertingScore {
    fn create_interpret(&self) -> Box<dyn crate::interpret::Interpret<T>> {
        let config = Arc::new(Mutex::new(KubePrometheusConfig::new()));
        config
            .try_lock()
            .expect("couldn't lock config")
            .additional_service_monitors = self.service_monitors.clone();
        Box::new(AlertingInterpret {
            sender: Prometheus::new(),
            receivers: self.receivers.clone(),
            rules: self.rules.clone(),
        })
    }
    fn name(&self) -> String {
        "HelmPrometheusAlertingScore".to_string()
    }
}
