# shellcheck shell=dash

___x_cmd_dns_reset___win_target(){
    local alias="$1"; shift
    [ -n "$alias" ] || N=dns M="Usage: x dns reset <alias>" log:ret:64

    if ! ___x_cmd hascmd powershell && ! ___x_cmd hascmd pwsh; then
        N=dns M="PowerShell not found" log:ret:64
    fi

    dns:info "Resetting DNS to DHCP for interface: $alias"
    ___x_cmd sudo powershell -NoProfile -Command "Set-DnsClientServerAddress -InterfaceAlias '$alias' -ResetServerAddresses" \
        || N=dns M="Failed to reset DNS via PowerShell" log:ret:1

    dns:info "DNS reset for interface: $alias"
}

___x_cmd_dns_reset___win(){
    local alias=""
    if [ $# -gt 0 ]; then
        case "$1" in
            *:*|*.*.*)  N=dns M="Target is not an IP ==> $1" log:ret:64 ;;
        esac
        alias="$1"; shift
    fi

    if [ -n "$alias" ]; then
        ___x_cmd_dns_reset___win_target "$alias"
        return
    fi

    if ! ___x_cmd hascmd powershell && ! ___x_cmd hascmd pwsh; then
        N=dns M="PowerShell not found" log:ret:64
    fi
    dns:info "Resetting DNS to DHCP on all interfaces"
    ___x_cmd sudo powershell -NoProfile -Command "Get-NetAdapter -Physical | Set-DnsClientServerAddress -ResetServerAddresses" \
        || N=dns M="Failed to reset DNS via PowerShell" log:ret:1
    dns:info "System-wide DNS reset"
}
