//! Global registry of admin roles, populated at boot by the generated code.
use std::sync::RwLock;

/// Global registry of admin roles declared via `admin!{}`.
///
/// Populated once at boot by `admin_register()` (code generated by the daemon).
/// Read-only thereafter — no contention.
static ADMIN_ROLES: RwLock<Vec<String>> = RwLock::new(Vec::new());

/// Registers admin roles at boot (called by generated code).
pub fn register_roles(roles: Vec<String>) {
    match ADMIN_ROLES.write() {
        Ok(mut guard) => *guard = roles,
        Err(_) => {
            if let Some(level) = crate::utils::runique_log::get_log().roles {
                crate::runique_log!(
                    level,
                    "register_roles(): impossible to acquire write lock — roles not registered"
                );
            }
        }
    }
}

/// Returns a copy of the registered roles.
pub fn get_roles() -> Vec<String> {
    if let Ok(guard) = ADMIN_ROLES.read() {
        guard.clone()
    } else {
        if let Some(level) = crate::utils::runique_log::get_log().roles {
            crate::runique_log!(level, "get_roles(): poisoned lock — returning empty roles");
        }
        Vec::new()
    }
}
