use anyhow::Result;
use std::path::Path;

/// Get the current hostname (lowercase)
///
/// On macOS, uses `scutil --get LocalHostName` which returns the stable Bonjour
/// hostname that doesn't change based on network/DHCP. Falls back to the system
/// hostname if LocalHostName is not available.
///
/// On other platforms, uses the system hostname directly.
pub fn get_hostname() -> String {
    #[cfg(target_os = "macos")]
    {
        // Try to get LocalHostName first (stable Bonjour name)
        if let Ok(output) = std::process::Command::new("scutil")
            .args(["--get", "LocalHostName"])
            .output()
        {
            if output.status.success() {
                let name = String::from_utf8_lossy(&output.stdout)
                    .trim()
                    .to_lowercase();
                if !name.is_empty() {
                    return name;
                }
            }
        }
    }

    // Fallback to system hostname
    hostname::get()
        .map(|h| h.to_string_lossy().to_lowercase())
        .unwrap_or_else(|_| "unknown".to_string())
}

/// Set file permissions to be readable/writable only by owner (600 on Unix)
#[cfg(unix)]
pub fn set_private_permissions(path: &Path) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;
    std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))?;
    Ok(())
}

/// Set file permissions on Windows using icacls
#[cfg(windows)]
pub fn set_private_permissions(path: &Path) -> Result<()> {
    use anyhow::Context;
    use std::process::Command;

    let path_str = path.to_string_lossy();

    // Get current username
    let username =
        std::env::var("USERNAME").with_context(|| "Failed to get USERNAME environment variable")?;

    // Remove inherited permissions and grant only current user full control
    let output = Command::new("icacls")
        .args([
            &*path_str,
            "/inheritance:r",
            "/grant:r",
            &format!("{}:F", username),
        ])
        .output()
        .with_context(|| "Failed to run icacls")?;

    if !output.status.success() {
        anyhow::bail!("icacls failed: {}", String::from_utf8_lossy(&output.stderr));
    }

    Ok(())
}

/// Get the home directory path string for use in SSH config
/// Returns %d (SSH config placeholder for home directory)
pub fn ssh_home_placeholder() -> &'static str {
    "%d"
}
