use super::MurConfig;
use super::MurConfigResult;
use super::MurEnv;
use super::MurEnvProfile;
use crate::server::error::MurError;

pub fn mur_load_config() -> MurConfig {
	MurConfig::from_env()
}

pub fn mur_load_config_required(required_keys: &[&str]) -> MurConfigResult<MurConfig> {
	let config = MurConfig::from_env();
	config.validate_required(required_keys)?;
	Ok(config)
}

pub fn mur_env(key: &'static str) -> Result<String, MurError> {
	std::env::var(key).map_err(|_| MurError::NoEnv(key))
}

pub fn mur_env_or(key: &str, default: &str) -> String {
	std::env::var(key).unwrap_or_else(|_| default.to_string())
}

pub fn mur_env_parse<T: std::str::FromStr>(key: &str) -> Option<T> {
	std::env::var(key).ok().and_then(|v| v.parse().ok())
}

pub fn mur_env_parse_or<T: std::str::FromStr>(key: &str, default: T) -> T {
	mur_env_parse(key).unwrap_or(default)
}

pub fn mur_env_is_true(key: &str) -> bool {
	std::env::var(key)
		.map(|v| {
			let v = v.to_lowercase();
			v == "1" || v == "true" || v == "yes" || v == "on"
		})
		.unwrap_or(false)
}

pub fn mur_current_env() -> MurEnvProfile {
	MurEnv::current()
}

pub fn mur_is_development() -> bool {
	mur_current_env().is_development()
}

pub fn mur_is_production() -> bool {
	mur_current_env().is_production()
}

pub fn mur_is_test() -> bool {
	mur_current_env().is_test()
}
