# bash completion for `wtf` / `wtftools`
# Enable:  eval "$(wtf completion bash)"   (add that line to ~/.bashrc),
#          or copy this script to /etc/bash_completion.d/wtf

_wtf_complete() {
    local cur prev cmd
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    local subcommands="info disk cpu mem net io who temp temps temperature daily audit problems crontab doctor services service config logs explain history diff top ports port docker events completion"
    local global_opts="-h --help -V --version -f --format --no-color -v --verbose -q --quiet --config"

    if [[ $COMP_CWORD -eq 1 ]]; then
        COMPREPLY=( $(compgen -W "$subcommands $global_opts" -- "$cur") )
        return 0
    fi

    cmd="${COMP_WORDS[1]}"

    case "$cmd" in
        info)
            COMPREPLY=( $(compgen -W "--format" -- "$cur") )
            [[ "$prev" == "--format" ]] && COMPREPLY=( $(compgen -W "text plain json" -- "$cur") )
            ;;
        daily)
            COMPREPLY=( $(compgen -W "--since --ignore --strict --exit-zero --format" -- "$cur") )
            [[ "$prev" == "--format" ]] && COMPREPLY=( $(compgen -W "text json" -- "$cur") )
            ;;
        disk)
            local opts="--tree --depth --top --format --show-commands"
            case "$prev" in
                --format)        COMPREPLY=( $(compgen -W "text plain json" -- "$cur") ); return 0 ;;
                --tree|--depth|--top) return 0 ;;
            esac
            if [[ "$cur" == -* ]]; then
                COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
            else
                COMPREPLY=( $(compgen -d -- "$cur") )   # a PATH to break down
            fi
            ;;
        cpu|net|io)
            COMPREPLY=( $(compgen -W "--format --show-commands" -- "$cur") )
            [[ "$prev" == "--format" ]] && COMPREPLY=( $(compgen -W "text plain json" -- "$cur") )
            ;;
        mem|who)
            COMPREPLY=( $(compgen -W "--since --format --show-commands" -- "$cur") )
            [[ "$prev" == "--format" ]] && COMPREPLY=( $(compgen -W "text plain json" -- "$cur") )
            ;;
        temp|temps|temperature)
            COMPREPLY=( $(compgen -W "--format" -- "$cur") )
            [[ "$prev" == "--format" ]] && COMPREPLY=( $(compgen -W "text plain json" -- "$cur") )
            ;;
        audit|problems)
            local opts="--format --strict --exit-zero --check --only --since --list-checks --brief -b --ignore --serial --check-timeout --alert --alert-on --save --output -o"
            case "$prev" in
                --format)   COMPREPLY=( $(compgen -W "text json prometheus csv plain html" -- "$cur") ); return 0 ;;
                --only)     COMPREPLY=( $(compgen -W "fail warn problems problem skip ok all" -- "$cur") ); return 0 ;;
                --alert-on) COMPREPLY=( $(compgen -W "fail warn any" -- "$cur") ); return 0 ;;
                --output|-o) COMPREPLY=( $(compgen -f -- "$cur") ); return 0 ;;
                --check|--ignore)
                    local names="uptime system load iowait psi tcp-retrans memory swap disks \
inodes readonly-mounts failed-units enabled-inactive restart-loops network-errors conntrack \
journal-disk zombies d-state oom kernel-errors kernel-taint cert-expiry fds pids auth time-sync \
updates reboot cron-daemon crontab docker hw-temp smart dns http-probes tcp-probes fail2ban"
                    COMPREPLY=( $(compgen -W "$names" -- "$cur") )
                    return 0
                    ;;
            esac
            COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
            ;;
        explain)
            local opts="--check --ignore --since --all --prompt --format --serial --check-timeout --llm --llm-model --llm-timeout"
            case "$prev" in
                --format) COMPREPLY=( $(compgen -W "text json" -- "$cur") ); return 0 ;;
                --llm)    COMPREPLY=( $(compgen -W "ollama claude openai auto" -- "$cur") ); return 0 ;;
                --check|--ignore)
                    local names="uptime system load iowait psi tcp-retrans memory swap disks \
inodes readonly-mounts failed-units enabled-inactive restart-loops network-errors conntrack \
journal-disk zombies d-state oom kernel-errors kernel-taint cert-expiry fds pids auth time-sync \
updates reboot cron-daemon crontab docker hw-temp smart dns http-probes tcp-probes fail2ban"
                    COMPREPLY=( $(compgen -W "$names" -- "$cur") )
                    return 0
                    ;;
            esac
            COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
            ;;
        diff)
            local opts="--snapshot --against --format"
            case "$prev" in
                --format)  COMPREPLY=( $(compgen -W "text json" -- "$cur") ); return 0 ;;
                --against) COMPREPLY=( $(compgen -f -- "$cur") ); return 0 ;;
            esac
            COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
            ;;
        history)
            COMPREPLY=( $(compgen -W "--limit --format" -- "$cur") )
            [[ "$prev" == "--format" ]] && COMPREPLY=( $(compgen -W "text json" -- "$cur") )
            ;;
        top)
            local opts="--sort --limit --user --name --format"
            case "$prev" in
                --sort)   COMPREPLY=( $(compgen -W "cpu rss" -- "$cur") ); return 0 ;;
                --format) COMPREPLY=( $(compgen -W "text plain json" -- "$cur") ); return 0 ;;
                --user)   COMPREPLY=( $(compgen -u -- "$cur") ); return 0 ;;
            esac
            COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
            ;;
        ports|port)
            local opts="--proto --public-only --format"
            case "$prev" in
                --proto)  COMPREPLY=( $(compgen -W "tcp udp all" -- "$cur") ); return 0 ;;
                --format) COMPREPLY=( $(compgen -W "text plain json" -- "$cur") ); return 0 ;;
            esac
            COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
            ;;
        docker)
            case "$prev" in
                --format) COMPREPLY=( $(compgen -W "text plain json" -- "$cur") ); return 0 ;;
            esac
            local names=$(docker ps --format '{{.Names}}' 2>/dev/null)
            COMPREPLY=( $(compgen -W "--format $names" -- "$cur") )
            ;;
        services|service)
            local opts="-n --lines --format"
            case "$prev" in
                --format) COMPREPLY=( $(compgen -W "text plain json" -- "$cur") ); return 0 ;;
            esac
            local units=$(systemctl list-units --type=service --no-legend --plain 2>/dev/null \
                          | awk '{ sub(/\.service$/, "", $1); print $1 }')
            COMPREPLY=( $(compgen -W "$opts $units" -- "$cur") )
            ;;
        events)
            local opts="--since --kind --limit --format"
            case "$prev" in
                --kind)   COMPREPLY=( $(compgen -W "reboot oom failed-unit kernel-err auth-fail login" -- "$cur") ); return 0 ;;
                --format) COMPREPLY=( $(compgen -W "text plain json" -- "$cur") ); return 0 ;;
            esac
            COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
            ;;
        logs)
            local opts="--since --priority -p --units --lines -n --format"
            case "$prev" in
                --format)      COMPREPLY=( $(compgen -W "text plain json" -- "$cur") ); return 0 ;;
                --priority|-p) COMPREPLY=( $(compgen -W "emerg alert crit err warning notice info debug" -- "$cur") ); return 0 ;;
            esac
            COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
            ;;
        doctor)
            COMPREPLY=( $(compgen -W "--format --check-updates" -- "$cur") )
            [[ "$prev" == "--format" ]] && COMPREPLY=( $(compgen -W "text json" -- "$cur") )
            ;;
        crontab)
            local opts="-S --system -U --user-file -u --username --format --strict --exit-zero"
            case "$prev" in
                -S|--system|-U|--user-file) COMPREPLY=( $(compgen -f -- "$cur") ); return 0 ;;
                -u|--username) COMPREPLY=( $(compgen -u -- "$cur") ); return 0 ;;
                --format)      COMPREPLY=( $(compgen -W "text json" -- "$cur") ); return 0 ;;
            esac
            COMPREPLY=( $(compgen -f -W "$opts" -- "$cur") )
            ;;
        config)
            COMPREPLY=( $(compgen -W "--example --format" -- "$cur") )
            [[ "$prev" == "--format" ]] && COMPREPLY=( $(compgen -W "text json" -- "$cur") )
            ;;
        completion)
            COMPREPLY=( $(compgen -W "bash zsh" -- "$cur") )
            ;;
    esac
}

complete -F _wtf_complete wtf
complete -F _wtf_complete wtftools
