//! Internal state management for ClusterNode.

use std::net::IpAddr;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;

use crate::metrics::{MetricsCollector, NoopMetricsCollector};
use crate::types::AtomicHealth;

/// Internal state for ClusterNode.
///
/// This struct is wrapped in an `Arc` to allow cheap cloning of `ClusterNode`.
pub(super) struct ClusterNodeInner {
    pub etcd_endpoints: Vec<String>,
    pub node_health: AtomicHealth,
    pub node_id: String,
    pub node_ip: IpAddr,
    pub group_name: String,
    pub ttl: i64,
    pub is_leader: AtomicBool,
    /// Metrics collector for reporting node metrics
    pub metrics_collector: Arc<dyn MetricsCollector>,
    /// Interval in seconds for metrics updates (0 = disabled)
    pub metrics_update_interval: u64,
}

impl ClusterNodeInner {
    /// Creates a new ClusterNodeInner with the given configuration.
    pub fn new(
        etcd_endpoints: Vec<String>,
        node_id: String,
        node_ip: IpAddr,
        group_name: String,
        ttl: i64,
    ) -> Self {
        Self::with_metrics(
            etcd_endpoints,
            node_id,
            node_ip,
            group_name,
            ttl,
            Arc::new(NoopMetricsCollector::new()),
            0,
        )
    }

    /// Creates a new ClusterNodeInner with a custom metrics collector.
    pub fn with_metrics(
        etcd_endpoints: Vec<String>,
        node_id: String,
        node_ip: IpAddr,
        group_name: String,
        ttl: i64,
        metrics_collector: Arc<dyn MetricsCollector>,
        metrics_update_interval: u64,
    ) -> Self {
        ClusterNodeInner {
            etcd_endpoints,
            node_health: AtomicHealth::new(),
            node_id,
            node_ip,
            group_name,
            ttl,
            is_leader: AtomicBool::new(false),
            metrics_collector,
            metrics_update_interval,
        }
    }
}
