use kube::CustomResource;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use super::crd_prometheuses::LabelSelector;

/// Rust CRD for `Alertmanager` from Prometheus Operator
#[derive(CustomResource, Serialize, Deserialize, Debug, Clone, JsonSchema)]
#[kube(
    group = "monitoring.coreos.com",
    version = "v1",
    kind = "Alertmanager",
    plural = "alertmanagers",
    namespaced
)]
#[serde(rename_all = "camelCase")]
pub struct AlertmanagerSpec {
    /// Number of replicas for HA
    pub replicas: i32,

    /// Selectors for AlertmanagerConfig CRDs
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub alertmanager_config_selector: Option<LabelSelector>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub alertmanager_config_namespace_selector: Option<LabelSelector>,

    /// Optional pod template metadata (annotations, labels)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pod_metadata: Option<LabelSelector>,

    /// Optional topology spread settings
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
}

impl Default for AlertmanagerSpec {
    fn default() -> Self {
        AlertmanagerSpec {
            replicas: 1,

            // Match all AlertmanagerConfigs in the same namespace
            alertmanager_config_namespace_selector: None,

            // Empty selector matches all AlertmanagerConfigs in that namespace
            alertmanager_config_selector: Some(LabelSelector::default()),

            pod_metadata: None,
            version: None,
        }
    }
}
