use serde::Serialize;

use crate::{
    interpret::Interpret,
    modules::dns::DnsScore,
    score::Score,
    topology::{DnsRecord, DnsRecordType, DnsServer, HAClusterTopology, Topology},
};

#[derive(Debug, Clone, Serialize)]
pub struct OKDDnsScore {
    dns_score: DnsScore,
}

impl OKDDnsScore {
    pub fn new(topology: &HAClusterTopology) -> Self {
        let cluster_domain_name = &topology.domain_name;
        let dns_entries = vec![
            DnsRecord {
                host: "api".to_string(),
                domain: cluster_domain_name.clone(),
                record_type: DnsRecordType::A,
                value: topology.dns_server.get_ip(),
            },
            DnsRecord {
                host: "api-int".to_string(),
                domain: cluster_domain_name.clone(),
                record_type: DnsRecordType::A,
                value: topology.dns_server.get_ip(),
            },
            DnsRecord {
                host: "*".to_string(),
                domain: format!("apps.{}", cluster_domain_name),
                record_type: DnsRecordType::A,
                value: topology.dns_server.get_ip(),
            },
        ];

        Self {
            dns_score: DnsScore::new(dns_entries, Some(true)),
        }
    }
}

impl<T: Topology + DnsServer> Score<T> for OKDDnsScore {
    fn create_interpret(&self) -> Box<dyn Interpret<T>> {
        self.dns_score.create_interpret()
    }

    fn name(&self) -> String {
        "OKDDnsScore".to_string()
    }
}
