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

    # Complete options
    case "$prev" in
        -f|--file)
            _filedir -y
            return
            ;;
        -v|--verbosity)
            COMPREPLY=($(compgen -W "0 1 2 3 4 5" -- "$cur"))
            return
            ;;
        --completion)
            COMPREPLY=($(compgen -W "bash fish zsh" -- "$cur"))
            return
            ;;
    esac

    # Complete task names if not an option
    if [[ "$cur" != -* ]]; then
        local tasks
        if hacefile="${HACEFILE:-Hacefile.yml}"; [[ -f "$hacefile" ]]; then
            tasks=$(hace --list 2>/dev/null | awk 'NR>3 && NF>0 {print $1}' | grep -v '^TASK\\|^---\\|^Legends')
        else
            tasks=()
        fi
        COMPREPLY=($(compgen -W "$tasks" -- "$cur"))
        return
    fi

    # Complete options
    local options="--file --dry-run --quiet --verbosity --always-make --keep-going --parallel --question --list --auto --completion --version --help -f -n -q -v -B -k -h"
    COMPREPLY=($(compgen -W "$options" -- "$cur"))
}

complete -F _hace_completion hace
