use std::time::Duration;

use crate::discovery_etcd::watch::BackoffConfig;

/// Optional etcd authentication configuration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EtcdAuthConfig {
    /// Username.
    pub username: String,
    /// Password.
    pub password: String,
}

/// etcd discovery adapter configuration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EtcdDiscoveryConfig {
    /// etcd endpoints.
    pub endpoints: Vec<String>,
    /// Key prefix.
    pub prefix: String,
    /// Lease TTL in seconds.
    pub lease_ttl: i64,
    /// Watch reconnect interval.
    pub reconnect_interval: Duration,
    /// Maximum time allowed for creating the gRPC connection.
    pub connect_timeout: Duration,
    /// Maximum time allowed for regular etcd operations.
    pub operation_timeout: Duration,
    /// Interval between lease keep-alive requests.
    pub keep_alive_interval: Duration,
    /// Maximum time allowed for one lease keep-alive response.
    pub keep_alive_timeout: Duration,
    /// Backoff used by watch reconnect loops.
    pub watch_backoff: BackoffConfig,
    /// Optional authentication.
    pub auth: Option<EtcdAuthConfig>,
}

impl Default for EtcdDiscoveryConfig {
    fn default() -> Self {
        Self {
            endpoints: vec!["http://127.0.0.1:2379".to_string()],
            prefix: "/rs-zero".to_string(),
            lease_ttl: 30,
            reconnect_interval: Duration::from_secs(1),
            connect_timeout: Duration::from_secs(3),
            operation_timeout: Duration::from_secs(3),
            keep_alive_interval: Duration::from_secs(5),
            keep_alive_timeout: Duration::from_secs(3),
            watch_backoff: BackoffConfig::default(),
            auth: None,
        }
    }
}
