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

pub static RUNTIME_CACHE_ENABLED: Lazy<RwLock<bool>> = Lazy::new(|| {
    let initial = env::var("MONGODB_CACHE_ENABLED")
        .unwrap_or_else(|_| "true".to_string())
        .parse()
        .unwrap_or(true);
    RwLock::new(initial)
});

pub fn get_cache_enabled() -> bool {
    *RUNTIME_CACHE_ENABLED.read().unwrap()
}

pub fn set_cache_enabled(enabled: bool) {
    *RUNTIME_CACHE_ENABLED.write().unwrap() = enabled;
    tracing::info!("Cache enabled set to: {}", enabled);
}

pub static DATABASE_URL: Lazy<String> = Lazy::new(|| {
    env::var("DATABASE_URL")
        .unwrap_or_else(|_| "postgres://accusys@localhost:5432/momentry".to_string())
});

pub static MONGODB_URL: Lazy<String> = Lazy::new(|| {
    env::var("MONGODB_URL").unwrap_or_else(|_| "mongodb://localhost:27017".to_string())
});

pub static REDIS_URL: Lazy<String> = Lazy::new(|| {
    env::var("REDIS_URL").unwrap_or_else(|_| {
        let password = env::var("REDIS_PASSWORD").unwrap_or_else(|_| "accusys".to_string());
        // Format: redis://[:password]@host:port (use default user)
        format!("redis://:{}@localhost:6379", password)
    })
});

pub static REDIS_PASSWORD: Lazy<String> =
    Lazy::new(|| env::var("REDIS_PASSWORD").unwrap_or_else(|_| "accusys".to_string()));

pub static OUTPUT_DIR: Lazy<String> = Lazy::new(|| {
    env::var("MOMENTRY_OUTPUT_DIR").unwrap_or_else(|_| "/Users/accusys/momentry/output".to_string())
});

pub static BACKUP_DIR: Lazy<String> = Lazy::new(|| {
    env::var("MOMENTRY_BACKUP_DIR")
        .unwrap_or_else(|_| "/Users/accusys/momentry/backup/momentry".to_string())
});

pub static PYTHON_PATH: Lazy<String> = Lazy::new(|| {
    env::var("MOMENTRY_PYTHON_PATH").unwrap_or_else(|_| "/opt/homebrew/bin/python3.11".to_string())
});

pub static SCRIPTS_DIR: Lazy<String> = Lazy::new(|| {
    env::var("MOMENTRY_SCRIPTS_DIR")
        .unwrap_or_else(|_| "/Users/accusys/momentry_core_0.1/scripts".to_string())
});

pub static LOG_LEVEL: Lazy<String> = Lazy::new(|| {
    env::var("RUST_LOG")
        .or_else(|_| env::var("MOMENTRY_LOG_LEVEL"))
        .unwrap_or_else(|_| "info".to_string())
});

pub static MEDIA_BASE_URL: Lazy<String> = Lazy::new(|| {
    env::var("MOMENTRY_MEDIA_BASE_URL")
        .unwrap_or_else(|_| "https://wp.momentry.ddns.net".to_string())
});

pub static SERVER_PORT: Lazy<u16> = Lazy::new(|| {
    env::var("MOMENTRY_SERVER_PORT")
        .unwrap_or_else(|_| "3002".to_string())
        .parse()
        .unwrap_or(3002)
});

pub static REDIS_KEY_PREFIX: Lazy<String> =
    Lazy::new(|| env::var("MOMENTRY_REDIS_PREFIX").unwrap_or_else(|_| "momentry:".to_string()));

pub static DATABASE_SCHEMA: Lazy<String> =
    Lazy::new(|| env::var("DATABASE_SCHEMA").unwrap_or_else(|_| "public".to_string()));

pub static MONGODB_DATABASE: Lazy<String> =
    Lazy::new(|| env::var("MONGODB_DATABASE").unwrap_or_else(|_| "momentry".to_string()));

pub static QDRANT_COLLECTION: Lazy<String> =
    Lazy::new(|| env::var("QDRANT_COLLECTION").unwrap_or_else(|_| "momentry_rule1".to_string()));

pub mod processor {
    use super::*;

    pub static ASR_TIMEOUT_SECS: Lazy<u64> = Lazy::new(|| {
        env::var("MOMENTRY_ASR_TIMEOUT")
            .unwrap_or_else(|_| "3600".to_string())
            .parse()
            .unwrap_or(3600)
    });

    pub static CUT_TIMEOUT_SECS: Lazy<u64> = Lazy::new(|| {
        env::var("MOMENTRY_CUT_TIMEOUT")
            .unwrap_or_else(|_| "3600".to_string())
            .parse()
            .unwrap_or(3600)
    });

    pub static DEFAULT_TIMEOUT_SECS: Lazy<u64> = Lazy::new(|| {
        env::var("MOMENTRY_DEFAULT_TIMEOUT")
            .unwrap_or_else(|_| "7200".to_string())
            .parse()
            .unwrap_or(7200)
    });
}

pub mod cache {
    use super::*;

    pub static MONGODB_CACHE_ENABLED: Lazy<bool> = Lazy::new(|| {
        env::var("MONGODB_CACHE_ENABLED")
            .unwrap_or_else(|_| "true".to_string())
            .parse()
            .unwrap_or(true)
    });

    pub static MONGODB_CACHE_TTL_VIDEOS: Lazy<u64> = Lazy::new(|| {
        env::var("MONGODB_CACHE_TTL_VIDEOS")
            .unwrap_or_else(|_| "300".to_string())
            .parse()
            .unwrap_or(300)
    });

    pub static MONGODB_CACHE_TTL_SEARCH: Lazy<u64> = Lazy::new(|| {
        env::var("MONGODB_CACHE_TTL_SEARCH")
            .unwrap_or_else(|_| "300".to_string())
            .parse()
            .unwrap_or(300)
    });

    pub static MONGODB_CACHE_TTL_HYBRID_SEARCH: Lazy<u64> = Lazy::new(|| {
        env::var("MONGODB_CACHE_TTL_HYBRID_SEARCH")
            .unwrap_or_else(|_| "600".to_string())
            .parse()
            .unwrap_or(600)
    });

    pub static MONGODB_CACHE_TTL_VIDEO_META: Lazy<u64> = Lazy::new(|| {
        env::var("MONGODB_CACHE_TTL_VIDEO_META")
            .unwrap_or_else(|_| "3600".to_string())
            .parse()
            .unwrap_or(3600)
    });

    pub static REDIS_CACHE_TTL_HEALTH: Lazy<u64> = Lazy::new(|| {
        env::var("REDIS_CACHE_TTL_HEALTH")
            .unwrap_or_else(|_| "30".to_string())
            .parse()
            .unwrap_or(30)
    });

    pub static REDIS_CACHE_TTL_VIDEO_META: Lazy<u64> = Lazy::new(|| {
        env::var("REDIS_CACHE_TTL_VIDEO_META")
            .unwrap_or_else(|_| "3600".to_string())
            .parse()
            .unwrap_or(3600)
    });
}

pub mod snapshot {
    use super::*;

    pub static SNAPSHOT_DIR_NAME: Lazy<String> = Lazy::new(|| {
        env::var("MOMENTRY_SNAPSHOT_DIR_NAME").unwrap_or_else(|_| ".momentry_snapshots".to_string())
    });

    pub static HOT_THRESHOLD: Lazy<u64> = Lazy::new(|| {
        env::var("MOMENTRY_SNAPSHOT_HOT_THRESHOLD")
            .unwrap_or_else(|_| "5".to_string())
            .parse()
            .unwrap_or(5)
    });

    pub static HOT_TTL_SECS: Lazy<u64> = Lazy::new(|| {
        env::var("MOMENTRY_SNAPSHOT_HOT_TTL_SECS")
            .unwrap_or_else(|_| "86400".to_string())
            .parse()
            .unwrap_or(86400)
    });

    pub static WARM_TTL_SECS: Lazy<u64> = Lazy::new(|| {
        env::var("MOMENTRY_SNAPSHOT_WARM_TTL_SECS")
            .unwrap_or_else(|_| "604800".to_string())
            .parse()
            .unwrap_or(604800)
    });

    pub static GENERATING_TIMEOUT_SECS: Lazy<u64> = Lazy::new(|| {
        env::var("MOMENTRY_SNAPSHOT_GENERATING_TIMEOUT")
            .unwrap_or_else(|_| "1800".to_string())
            .parse()
            .unwrap_or(1800)
    });
}

pub mod llm {
    use super::*;

    pub static SUMMARY_URL: Lazy<String> = Lazy::new(|| {
        env::var("MOMENTRY_LLM_SUMMARY_URL")
            .unwrap_or_else(|_| "http://127.0.0.1:8081/v1/chat/completions".to_string())
    });

    pub static SUMMARY_MODEL: Lazy<String> = Lazy::new(|| {
        env::var("MOMENTRY_LLM_SUMMARY_MODEL").unwrap_or_else(|_| "gemma4".to_string())
    });

    pub static SUMMARY_TIMEOUT_SECS: Lazy<u64> = Lazy::new(|| {
        env::var("MOMENTRY_LLM_SUMMARY_TIMEOUT")
            .unwrap_or_else(|_| "120".to_string())
            .parse()
            .unwrap_or(120)
    });

    pub static SUMMARY_ENABLED: Lazy<bool> = Lazy::new(|| {
        env::var("MOMENTRY_LLM_SUMMARY_ENABLED")
            .map(|v| v == "true" || v == "1")
            .unwrap_or(true)
    });
}
