# bash completion for vinput

_vinput_installed_models() {
    vinput -j model list 2>/dev/null | grep -o '"id": *"[^"]*"' | cut -d'"' -f4
}

_vinput_installed_scenes() {
    vinput -j scene list 2>/dev/null | grep -o '"id": *"[^"]*"' | cut -d'"' -f4
}

_vinput_installed_providers() {
    vinput -j provider list 2>/dev/null | grep -o '"id": *"[^"]*"' | cut -d'"' -f4
}

_vinput_installed_llm_providers() {
    vinput -j llm list 2>/dev/null | grep -o '"id": *"[^"]*"' | cut -d'"' -f4
}

_vinput_installed_adapters() {
    vinput -j adapter list 2>/dev/null | grep -o '"id": *"[^"]*"' | cut -d'"' -f4
}

_vinput() {
    local cur prev words cword
    if declare -F _init_completion >/dev/null 2>&1; then
        _init_completion || return
    else
        COMPREPLY=()
        cur="${COMP_WORDS[COMP_CWORD]}"
        prev="${COMP_WORDS[COMP_CWORD-1]}"
        words=("${COMP_WORDS[@]}")
        cword=$COMP_CWORD
    fi

    local global_opts="-j --json -h --help -v --version"
    local subcommands="init model provider llm adapter hotword device scene config daemon recording rec"

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

    local cmd="${words[1]}"

    case "$cmd" in
        init)
            COMPREPLY=($(compgen -W "-f --force -h --help" -- "$cur"))
            return 0
            ;;
        model)
            if [[ $cword -eq 2 ]]; then
                if [[ "$cur" == -* ]]; then
                    COMPREPLY=($(compgen -W "-h --help" -- "$cur"))
                else
                    COMPREPLY=($(compgen -W "list ls add use remove rm info" -- "$cur"))
                fi
            else
                case "${words[2]}" in
                    list|ls)
                        COMPREPLY=($(compgen -W "-a --available -h --help" -- "$cur"))
                        ;;
                    use|remove|rm|info)
                        if [[ "$cur" == -* ]]; then
                            COMPREPLY=($(compgen -W "-h --help" -- "$cur"))
                        else
                            COMPREPLY=($(compgen -W "$(_vinput_installed_models)" -- "$cur"))
                        fi
                        ;;
                esac
            fi
            return 0
            ;;
        provider)
            if [[ $cword -eq 2 ]]; then
                if [[ "$cur" == -* ]]; then
                    COMPREPLY=($(compgen -W "-h --help" -- "$cur"))
                else
                    COMPREPLY=($(compgen -W "list ls add use edit e remove rm" -- "$cur"))
                fi
            else
                case "${words[2]}" in
                    list|ls)
                        COMPREPLY=($(compgen -W "-a --available -h --help" -- "$cur"))
                        ;;
                    use|edit|e|remove|rm)
                        if [[ "$cur" == -* ]]; then
                            COMPREPLY=($(compgen -W "-h --help" -- "$cur"))
                        else
                            COMPREPLY=($(compgen -W "$(_vinput_installed_providers)" -- "$cur"))
                        fi
                        ;;
                esac
            fi
            return 0
            ;;
        llm)
            if [[ $cword -eq 2 ]]; then
                if [[ "$cur" == -* ]]; then
                    COMPREPLY=($(compgen -W "-h --help" -- "$cur"))
                else
                    COMPREPLY=($(compgen -W "list ls add edit e remove rm test" -- "$cur"))
                fi
            else
                case "${words[2]}" in
                    add)
                        COMPREPLY=($(compgen -W "-u --base-url -k --api-key -e --extra-body -h --help" -- "$cur"))
                        ;;
                    edit|e|remove|rm|test)
                        if [[ "$cur" == -* ]]; then
                            COMPREPLY=($(compgen -W "-u --base-url -k --api-key -e --extra-body -h --help" -- "$cur"))
                        else
                            COMPREPLY=($(compgen -W "$(_vinput_installed_llm_providers)" -- "$cur"))
                        fi
                        ;;
                esac
            fi
            return 0
            ;;
        adapter)
            if [[ $cword -eq 2 ]]; then
                if [[ "$cur" == -* ]]; then
                    COMPREPLY=($(compgen -W "-h --help" -- "$cur"))
                else
                    COMPREPLY=($(compgen -W "list ls ps status add start stop restart enable disable" -- "$cur"))
                fi
            else
                case "${words[2]}" in
                    list|ls)
                        COMPREPLY=($(compgen -W "-a --available -h --help" -- "$cur"))
                        ;;
                    start|stop|restart|enable|disable|status)
                        if [[ "$cur" == -* ]]; then
                            COMPREPLY=($(compgen -W "-h --help" -- "$cur"))
                        else
                            COMPREPLY=($(compgen -W "$(_vinput_installed_adapters)" -- "$cur"))
                        fi
                        ;;
                esac
            fi
            return 0
            ;;
        hotword)
            if [[ $cword -eq 2 ]]; then
                if [[ "$cur" == -* ]]; then
                    COMPREPLY=($(compgen -W "-h --help" -- "$cur"))
                else
                    COMPREPLY=($(compgen -W "get set clear edit e" -- "$cur"))
                fi
            elif [[ "${words[2]}" == "set" ]]; then
                if declare -F _filedir >/dev/null 2>&1; then
                    _filedir
                else
                    COMPREPLY=($(compgen -f -- "$cur"))
                fi
            fi
            return 0
            ;;
        device)
            if [[ $cword -eq 2 ]]; then
                if [[ "$cur" == -* ]]; then
                    COMPREPLY=($(compgen -W "-h --help" -- "$cur"))
                else
                    COMPREPLY=($(compgen -W "list ls use" -- "$cur"))
                fi
            fi
            return 0
            ;;
        scene)
            if [[ $cword -eq 2 ]]; then
                if [[ "$cur" == -* ]]; then
                    COMPREPLY=($(compgen -W "-h --help" -- "$cur"))
                else
                    COMPREPLY=($(compgen -W "list ls add edit e use remove rm" -- "$cur"))
                fi
            else
                case "${words[2]}" in
                    add|edit|e)
                        if [[ "$cur" == -* ]]; then
                            COMPREPLY=($(compgen -W "--id -l --label -t --prompt -p --provider -m --model -c --count --timeout --context-lines -h --help" -- "$cur"))
                        else
                            COMPREPLY=($(compgen -W "$(_vinput_installed_scenes)" -- "$cur"))
                        fi
                        ;;
                    use|remove|rm)
                        if [[ "$cur" == -* ]]; then
                            COMPREPLY=($(compgen -W "-h --help" -- "$cur"))
                        else
                            COMPREPLY=($(compgen -W "$(_vinput_installed_scenes)" -- "$cur"))
                        fi
                        ;;
                esac
            fi
            return 0
            ;;
        config)
            if [[ $cword -eq 2 ]]; then
                if [[ "$cur" == -* ]]; then
                    COMPREPLY=($(compgen -W "-h --help" -- "$cur"))
                else
                    COMPREPLY=($(compgen -W "get set edit e" -- "$cur"))
                fi
            else
                case "${words[2]}" in
                    edit|e)
                        COMPREPLY=($(compgen -W "core fcitx" -- "$cur"))
                        ;;
                    set)
                        COMPREPLY=($(compgen -W "-i --stdin" -- "$cur"))
                        ;;
                esac
            fi
            return 0
            ;;
        daemon)
            if [[ $cword -eq 2 ]]; then
                if [[ "$cur" == -* ]]; then
                    COMPREPLY=($(compgen -W "-h --help" -- "$cur"))
                else
                    COMPREPLY=($(compgen -W "status start stop restart log" -- "$cur"))
                fi
            else
                case "${words[2]}" in
                    log)
                        COMPREPLY=($(compgen -W "-f --follow -n --lines -h --help" -- "$cur"))
                        ;;
                esac
            fi
            return 0
            ;;
        recording|rec)
            if [[ $cword -eq 2 ]]; then
                if [[ "$cur" == -* ]]; then
                    COMPREPLY=($(compgen -W "-h --help" -- "$cur"))
                else
                    COMPREPLY=($(compgen -W "start stop toggle" -- "$cur"))
                fi
            else
                case "${words[2]}" in
                    stop|toggle)
                        COMPREPLY=($(compgen -W "-s --scene -h --help" -- "$cur"))
                        ;;
                esac
            fi
            return 0
            ;;
    esac
}

complete -F _vinput vinput
