# okcontrib bash completion — loaded via bash-completion
_okcontrib_complete() {
    local cur prev commands
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    commands="auth onboard sig issue repo stats"

    # 全局参数
    local global_opts="-h --help -V --version"

    # 子命令参数
    local sig_actions="list show"
    local issue_actions="list recommend"
    local repo_actions="fork branch pr"
    local stats_actions="overview recent review"

    # 如果正在输入第一个子命令
    if [ $COMP_CWORD -eq 1 ]; then
        COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
        return 0
    fi

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

    case "${cmd}" in
        auth)
            # auth 后面只有 token 位置参数，无补全
            ;;
        onboard)
            # onboard 无额外参数
            ;;
        sig)
            if [ $COMP_CWORD -eq 2 ]; then
                COMPREPLY=( $(compgen -W "${sig_actions}" -- "${cur}") )
            fi
            ;;
        issue)
            if [ $COMP_CWORD -eq 2 ]; then
                COMPREPLY=( $(compgen -W "${issue_actions}" -- "${cur}") )
            else
                case "${prev}" in
                    -o|--owner) ;;
                    -r|--repo) ;;
                    -l|--labels) ;;
                    *)
                        COMPREPLY=( $(compgen -W "-o --owner -r --repo -l --labels" -- "${cur}") )
                        ;;
                esac
            fi
            ;;
        repo)
            if [ $COMP_CWORD -eq 2 ]; then
                COMPREPLY=( $(compgen -W "${repo_actions}" -- "${cur}") )
            else
                case "${prev}" in
                    -o|--owner|-r|--repo|-n|--name|--ref|-t|--title|-b|--body|--head|--base) ;;
                    *)
                        COMPREPLY=( $(compgen -W "-o --owner -r --repo -n --name --ref -t --title -b --body --head --base" -- "${cur}") )
                        ;;
                esac
            fi
            ;;
        stats)
            if [ $COMP_CWORD -eq 2 ]; then
                COMPREPLY=( $(compgen -W "${stats_actions}" -- "${cur}") )
            else
                case "${prev}" in
                    -u|--user|-d|--days) ;;
                    *)
                        COMPREPLY=( $(compgen -W "-u --user -d --days" -- "${cur}") )
                        ;;
                esac
            fi
            ;;
    esac
    return 0
}

complete -F _okcontrib_complete okcontrib
