# bash completion for mx-power-mgmt
# SPDX-License-Identifier: GPL-2.0-or-later

_mx_power_mgmt() {
    local cur prev words cword
    _init_completion || return

    # Find which module was specified (first non-option word after the binary)
    local module="" action=""
    local i
    for (( i = 1; i < cword; i++ )); do
        [[ "${words[i]}" == -* ]] && continue
        if [[ -z "$module" ]]; then
            module="${words[i]}"
        elif [[ -z "$action" ]]; then
            action="${words[i]}"
            break
        fi
    done

    # -----------------------------------------------------------------------
    # No module yet — complete modules and top-level flags
    # -----------------------------------------------------------------------
    if [[ -z "$module" ]]; then
        COMPREPLY=( $(compgen -W "system power cpu thermal --help --version --json" -- "$cur") )
        return
    fi

    # -----------------------------------------------------------------------
    # Module known, no action yet — complete actions for that module
    # -----------------------------------------------------------------------
    if [[ -z "$action" ]]; then
        case "$module" in
            system)
                COMPREPLY=( $(compgen -W "show set-mode set-wakeup-source --help" -- "$cur") ) ;;
            power)
                COMPREPLY=( $(compgen -W "list show set-auto-shutdown set-shutdown-delay reset-soh --help" -- "$cur") ) ;;
            cpu)
                COMPREPLY=( $(compgen -W "list show set-governor --help" -- "$cur") ) ;;
            thermal)
                COMPREPLY=( $(compgen -W "list show --help" -- "$cur") ) ;;
        esac
        return
    fi

    # -----------------------------------------------------------------------
    # Module + action known — complete arguments and options
    # -----------------------------------------------------------------------
    case "$module" in

        system)
            case "$action" in
                show)
                    COMPREPLY=( $(compgen -W "--json" -- "$cur") ) ;;
                set-mode)
                    case "$prev" in
                        -t|--timer) ;;  # expects a number
                        *)
                            COMPREPLY=( $(compgen -W "active conservation standby --timer --json" -- "$cur") ) ;;
                    esac ;;
                set-wakeup-source)
                    COMPREPLY=( $(compgen -W "enable disable --force --json" -- "$cur") ) ;;
            esac ;;

        power)
            case "$action" in
                list)
                    COMPREPLY=( $(compgen -W "--json" -- "$cur") ) ;;
                show)
                    COMPREPLY=( $(compgen -W "--json" -- "$cur") ) ;;
                set-auto-shutdown)
                    COMPREPLY=( $(compgen -W "enabled disabled" -- "$cur") ) ;;
                set-shutdown-delay)
                    ;; # expects a number
                reset-soh)
                    COMPREPLY=( $(compgen -W "--yes --json" -- "$cur") ) ;;
            esac ;;

        cpu)
            case "$action" in
                list)
                    COMPREPLY=( $(compgen -W "--json" -- "$cur") ) ;;
                show)
                    COMPREPLY=( $(compgen -W "--json" -- "$cur") ) ;;
                set-governor)
                    case "$prev" in
                        set-governor)
                            COMPREPLY=( $(compgen -W "performance powersave schedutil" -- "$cur") ) ;;
                        performance|powersave|schedutil)
                            COMPREPLY=( $(compgen -W "--json" -- "$cur") ) ;;
                        *)
                            COMPREPLY=( $(compgen -W "performance powersave schedutil --json" -- "$cur") ) ;;
                    esac ;;
            esac ;;

        thermal)
            case "$action" in
                list)
                    COMPREPLY=( $(compgen -W "--json" -- "$cur") ) ;;
                show)
                    COMPREPLY=( $(compgen -W "--json" -- "$cur") ) ;;
            esac ;;

    esac
}

complete -F _mx_power_mgmt mx-power-mgmt
