#![allow(dead_code)]

use super::{Platform, PlatformInfo};
use crate::error::AppResult;

pub fn get_platform_info() -> AppResult<PlatformInfo> {
    Ok(PlatformInfo {
        platform: Platform::MacOS,
        version: "14.0".to_string(), // TODO: Read actual macOS version
        supports_ios_supervision: true,
    })
}

pub fn install_dns_profile(profile_path: &str) -> AppResult<()> {
    use std::process::Command;
    let status = Command::new("open")
        .arg(profile_path)
        .status()
        .map_err(|e| crate::error::AppError::new(format!("Failed to open profile: {}", e)))?;

    if !status.success() {
        return Err(crate::error::AppError::new(
            "System Settings konnte das Profil nicht öffnen".to_string(),
        ));
    }
    Ok(())
}

pub fn store_token(_token: &str) -> AppResult<()> {
    // TODO: Implement using macOS Keychain via keyring crate
    Ok(())
}

pub fn read_token() -> AppResult<Option<String>> {
    // TODO: Implement using macOS Keychain via keyring crate
    Ok(None)
}

pub fn activate_protection(profile_path: &str) -> AppResult<()> {
    install_dns_profile(profile_path)
}

pub fn deactivate_protection(_token: &str) -> AppResult<()> {
    Ok(())
}

/// Returns the macOS system UUID (IOPlatformUUID).
pub fn get_hardware_id() -> AppResult<String> {
    use std::process::Command;
    let output = Command::new("ioreg")
        .args([
            "-rd1",
            "-c",
            "IOPlatformExpertDevice",
            "-k",
            "IOPlatformUUID",
        ])
        .output()
        .map_err(|e| crate::error::AppError::new(format!("Failed to read hardware ID: {}", e)))?;

    let text = String::from_utf8_lossy(&output.stdout);
    for line in text.lines() {
        if line.contains("IOPlatformUUID") {
            let parts: Vec<&str> = line.split('"').collect();
            if let Some(uuid) = parts.iter().rev().find(|s| s.contains('-')) {
                return Ok(uuid.trim().to_string());
            }
        }
    }

    Err(crate::error::AppError::new(
        "Could not parse macOS hardware UUID".to_string(),
    ))
}
