use std::{
    path::{Path, PathBuf},
    process::Command,
};

use tauri::{AppHandle, Manager};

pub fn current_tunnel_strategy() -> &'static str {
    if cfg!(target_os = "windows") {
        "embedded-wireguard-windows-x64"
    } else if cfg!(target_os = "macos") {
        "embedded-wireguard-macos-arm"
    } else {
        "unsupported"
    }
}

pub fn connect(app: &AppHandle, profile_path: &Path) -> Result<(), String> {
    let backend = bundled_backend(app)?;
    let output = Command::new(backend)
        .arg("connect")
        .arg("--profile")
        .arg(profile_path)
        .output()
        .map_err(|err| format!("Unable to start embedded tunnel backend: {}", err))?;

    if !output.status.success() {
        return Err(format_helper_error("connect", &output));
    }

    Ok(())
}

pub fn disconnect(app: &AppHandle, profile_path: &Path) -> Result<(), String> {
    let backend = bundled_backend(app)?;
    let output = Command::new(backend)
        .arg("disconnect")
        .arg("--profile")
        .arg(profile_path)
        .output()
        .map_err(|err| format!("Unable to stop embedded tunnel backend: {}", err))?;

    if !output.status.success() {
        return Err(format_helper_error("disconnect", &output));
    }

    Ok(())
}

fn bundled_backend(app: &AppHandle) -> Result<PathBuf, String> {
    let resource_dir = app
        .path()
        .resource_dir()
        .map_err(|err| format!("Unable to resolve resource dir: {}", err))?;

    let relative = if cfg!(target_os = "windows") {
        PathBuf::from("bundled/windows-x64/nexavpn-tunnel-helper.exe")
    } else if cfg!(target_os = "macos") {
        PathBuf::from("bundled/macos-arm64/nexavpn-tunnel-helper")
    } else {
        return Err("This NexaVPN client build supports embedded tunnel backends only for Windows x64 and macOS ARM".into());
    };

    let path = resource_dir.join(relative);
    if !path.exists() {
        return Err("Embedded NexaVPN tunnel backend is not bundled in this build yet.".into());
    }

    Ok(path)
}

fn format_helper_error(action: &str, output: &std::process::Output) -> String {
    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = String::from_utf8_lossy(&output.stdout);
    let details = if !stderr.trim().is_empty() {
        stderr.trim()
    } else {
        stdout.trim()
    };

    if details.is_empty() {
        return format!("NexaVPN tunnel backend {} failed with status {}", action, output.status);
    }

    details.to_string()
}
