# nordvpn_disconnect
# ------------------
# Desconecta de NordVPN. Idempotente — si no hay conexion activa, retorna ok.
# Imprime JSON con el resultado.
#
# USO (sourced):
#   source nordvpn_disconnect.sh
#   nordvpn_disconnect

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

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

    if [ $rc -eq 0 ] || echo "$output" | grep -qi "not connected\|disconnected"; then
        echo '{"ok":true,"status":"disconnected"}'
    else
        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
}
