# nordvpn_status
# --------------
# Obtiene el estado actual de NordVPN como JSON estructurado.
# Parsea la salida clave-valor de `nordvpn status` a campos JSON.
#
# USO (sourced):
#   source nordvpn_status.sh
#   nordvpn_status

nordvpn_status() {
    if ! command -v nordvpn &>/dev/null; then
        echo '{"ok":false,"error":"nordvpn no instalado"}' >&2
        return 1
    fi

    local output
    output=$(nordvpn status 2>&1)
    local rc=$?

    if [ $rc -ne 0 ]; then
        echo "{\"ok\":false,\"error\":$(echo "$output" | python3 -c 'import sys,json; print(json.dumps(sys.stdin.read().strip()))' 2>/dev/null || echo "\"$output\"")}"
        return 1
    fi

    # Parsear output clave: valor a JSON con python3
    echo "$output" | python3 -c '
import sys, json, re

lines = sys.stdin.read().strip().split("\n")
data = {"ok": True}
for line in lines:
    line = re.sub(r"\x1b\[[0-9;]*m", "", line).strip()
    line = line.lstrip("- ")
    if ":" in line:
        key, _, val = line.partition(":")
        key = key.strip().lower().replace(" ", "_")
        val = val.strip()
        if key == "status":
            data["connected"] = val.lower() == "connected"
        data[key] = val
print(json.dumps(data))
'
}
