use once_cell::sync::Lazy;
use std::env;

#[derive(Debug, Clone)]
pub struct WorkerConfig {
    pub max_concurrent: usize,
    pub poll_interval_secs: u64,
    pub enabled: bool,
    pub batch_size: i32,
    pub processor_timeout_secs: u64,
}

impl Default for WorkerConfig {
    fn default() -> Self {
        Self {
            max_concurrent: Self::max_concurrent_from_env(),
            poll_interval_secs: Self::poll_interval_from_env(),
            enabled: Self::enabled_from_env(),
            batch_size: Self::batch_size_from_env(),
            processor_timeout_secs: Self::timeout_from_env(),
        }
    }
}

impl WorkerConfig {
    fn max_concurrent_from_env() -> usize {
        env::var("MOMENTRY_MAX_CONCURRENT")
            .unwrap_or_else(|_| "2".to_string())
            .parse()
            .unwrap_or(2)
    }

    fn poll_interval_from_env() -> u64 {
        env::var("MOMENTRY_POLL_INTERVAL")
            .unwrap_or_else(|_| "5".to_string())
            .parse()
            .unwrap_or(5)
    }

    fn enabled_from_env() -> bool {
        env::var("MOMENTRY_WORKER_ENABLED")
            .unwrap_or_else(|_| "true".to_string())
            .parse()
            .unwrap_or(true)
    }

    fn batch_size_from_env() -> i32 {
        env::var("MOMENTRY_WORKER_BATCH_SIZE")
            .unwrap_or_else(|_| "10".to_string())
            .parse()
            .unwrap_or(10)
    }

    fn timeout_from_env() -> u64 {
        env::var("MOMENTRY_WORKER_PROCESSOR_TIMEOUT")
            .unwrap_or_else(|_| "3600".to_string())
            .parse()
            .unwrap_or(3600)
    }
}

pub static WORKER_CONFIG: Lazy<WorkerConfig> = Lazy::new(WorkerConfig::default);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_worker_config_default() {
        let config = WorkerConfig::default();
        assert_eq!(config.max_concurrent, 2);
        assert_eq!(config.poll_interval_secs, 5);
        assert!(config.enabled);
        assert_eq!(config.batch_size, 10);
        assert_eq!(config.processor_timeout_secs, 3600);
    }
}
