//! Stabile Device-Identität — analog Mac-App (Hardware-UUID),
//! auf Windows via MachineGuid aus der Registry.

#[cfg(windows)]
use protection_core::ps_run;

/// Stabile Device-ID: "win-<MachineGuid>".
/// MachineGuid übersteht Reinstalls der App (nicht aber Windows-Neuinstall).
pub fn device_id() -> String {
    #[cfg(windows)]
    {
        if let Ok(out) = ps_run(
            "(Get-ItemProperty 'HKLM:\\SOFTWARE\\Microsoft\\Cryptography').MachineGuid",
        ) {
            let guid = out.trim();
            if !guid.is_empty() {
                return format!("win-{guid}");
            }
        }
        // Fallback: Hostname-basiert (sollte praktisch nie passieren)
        format!("win-{}", hostname())
    }
    #[cfg(not(windows))]
    {
        format!("win-dev-{}", hostname())
    }
}

pub fn hostname() -> String {
    std::env::var("COMPUTERNAME")
        .or_else(|_| std::env::var("HOSTNAME"))
        .unwrap_or_else(|_| "Windows-PC".into())
}

pub fn os_version() -> String {
    #[cfg(windows)]
    {
        ps_run("[System.Environment]::OSVersion.VersionString")
            .map(|s| s.trim().to_string())
            .unwrap_or_else(|_| "Windows".into())
    }
    #[cfg(not(windows))]
    {
        "dev".into()
    }
}
