//! NetworkManager queries, via `nmcli`.

use crate::services::process;

/// Whether a cellular (gsm) data connection is active.
pub fn cellular_data_active() -> bool {
    process::output("nmcli", &["-t", "-f", "TYPE,STATE", "connection", "show", "--active"])
        .map(|s| s.contains("gsm:activated"))
        .unwrap_or(false)
}

/// Whether a VPN-like connection (vpn/wireguard/tun) is active.
pub fn vpn_active() -> bool {
    let Some(out) = process::output(
        "nmcli",
        &["-t", "-f", "TYPE,NAME,STATE", "connection", "show", "--active"],
    ) else {
        return false;
    };
    out.lines().any(|line| {
        matches!(line.split(':').next().unwrap_or(""), "vpn" | "wireguard" | "tun")
    })
}

/// Overall connectivity state (e.g. "full", "portal", "limited", "none").
pub fn connectivity() -> Option<String> {
    process::output("nmcli", &["-t", "-f", "CONNECTIVITY", "general"])
}

/// Raw `nmcli general status` output.
pub fn general_status() -> Option<String> {
    process::output("nmcli", &["general", "status"])
}
