use crate::config::ResolvedConfig;

/// Resolves the API secret from: CLI flag > env var > config file.
pub fn resolve_secret(flag: Option<&str>, config: &ResolvedConfig) -> anyhow::Result<String> {
    if let Some(secret) = flag {
        return Ok(secret.to_string());
    }

    if let Ok(secret) = std::env::var("BPX_API_SECRET") {
        return Ok(secret);
    }

    config.api_secret.clone().ok_or_else(|| {
        anyhow::anyhow!("Not authenticated. Run `bpx setup` to configure your API key.")
    })
}

/// Resolves the base URL from: CLI flag > env var > config file.
pub fn resolve_base_url(flag: Option<&str>, config: &ResolvedConfig) -> String {
    if let Some(url) = flag {
        return url.to_string();
    }

    if let Ok(url) = std::env::var("BPX_BASE_URL") {
        return url;
    }

    config.base_url.clone()
}
