//! Global application configuration set once at startup.
//!
//! Values are stored in `OnceLock` statics and accessed via the corresponding
//! getter functions.  All setters accept `Option` and silently skip `None`.

use std::sync::OnceLock;

/// Package name read from `Cargo.toml` at compile time.
pub const PKG_NAME: &str = env!("CARGO_PKG_NAME");

/// Package version read from `Cargo.toml` at compile time.
pub const PKG_VERSION: &str = env!("CARGO_PKG_VERSION");

static HOSTNAME: OnceLock<String> = OnceLock::new();
static SSH_PREFIX: OnceLock<String> = OnceLock::new();
static BASE_URL: OnceLock<String> = OnceLock::new();
static FAVICON: OnceLock<Vec<u8>> = OnceLock::new();

/// Returns the configured hostname, or `"index"` if none was provided.
pub fn hostname() -> &'static str {
    HOSTNAME.get().map_or("index", |s| s.as_str())
}

/// Returns the configured SSH prefix, or an empty string if none was provided.
pub fn ssh_prefix() -> &'static str {
    SSH_PREFIX.get().map_or("", |s| s.as_str())
}

/// Returns the configured base URL path prefix, or an empty string if none was
/// provided.
pub fn base_url() -> &'static str {
    BASE_URL.get().map_or("", |s| s.as_str())
}

/// Returns the favicon bytes loaded at startup.
pub fn favicon() -> &'static [u8] {
    FAVICON.get().map_or(&[], |v| v.as_slice())
}

/// Sets the hostname shown in breadcrumbs and clone URLs.
/// Silently skips `None`.
pub fn set_hostname(v: Option<String>) {
    if let Some(v) = v {
        let _ = HOSTNAME.set(v);
    }
}

/// Sets the path prefix used in SSH clone URLs.  Silently skips `None`.
pub fn set_ssh_prefix(v: Option<String>) {
    if let Some(v) = v {
        let _ = SSH_PREFIX.set(v);
    }
}

/// Sets the URL path prefix for reverse-proxy deployments.
/// Silently skips `None`.
pub fn set_base_url(v: Option<String>) {
    if let Some(v) = v {
        let _ = BASE_URL.set(v);
    }
}

/// Sets the favicon bytes served at `/favicon.ico`.
pub fn set_favicon(v: Vec<u8>) {
    let _ = FAVICON.set(v);
}
