_haj_installed_pkgs() {
    if command -v pacman >/dev/null 2>&1; then
        pacman -Qq 2>/dev/null
    elif command -v dpkg-query >/dev/null 2>&1; then
        dpkg-query -W -f='${Package}\n' 2>/dev/null
    elif command -v rpm >/dev/null 2>&1; then
        rpm -qa --qf '%{NAME}\n' 2>/dev/null
    fi
}

_haj_repo_pkgs() {
    if command -v pacman >/dev/null 2>&1; then
        pacman -Slq 2>/dev/null
    elif command -v apt-cache >/dev/null 2>&1; then
        apt-cache pkgnames 2>/dev/null
    elif command -v dnf5 >/dev/null 2>&1; then
        dnf5 -C -q repoquery --qf '%{name}\n' 2>/dev/null
    elif command -v dnf >/dev/null 2>&1; then
        dnf -C -q repoquery --qf '%{name}\n' 2>/dev/null
    fi
}

_haj_groups() {
    if command -v pacman >/dev/null 2>&1; then
        pacman -Sg 2>/dev/null
    elif command -v dnf5 >/dev/null 2>&1; then
        dnf5 -C -q group list 2>/dev/null | awk 'NR>1 {print $1}'
    elif command -v dnf >/dev/null 2>&1; then
        dnf -C -q group list --ids 2>/dev/null | sed -n 's/.*(\([^)]*\)).*/\1/p'
    fi
}

_haj_downgrade_pkgs() {
    (
        if [ -d /var/cache/pacman/pkg ]; then
            ls /var/cache/pacman/pkg/ 2>/dev/null | grep -E '\.pkg\.tar\.(zst|xz|gz|bz2|lz4|lzo)$' | sed -E 's/-[^-]+-[^-]+-[^-]+\.pkg\.tar\..*$//'
        fi
        if [ -d ~/.cache/haj/aur ]; then
            ls ~/.cache/haj/aur/ 2>/dev/null
        fi
        if [ -d /var/cache/apt/archives ]; then
            find /var/cache/apt/archives -maxdepth 1 -type f -name '*.deb' 2>/dev/null | xargs -r -n1 basename | sed -E 's/_[^_]+_[^_]+\.deb$//'
        fi
        if [ -d /var/cache/libdnf5 ] || [ -d /var/cache/dnf ]; then
            find /var/cache/libdnf5 /var/cache/dnf -type f -name '*.rpm' 2>/dev/null | xargs -r -n1 basename | sed -E 's/-[^-]+-[^-]+\.[^.]+\.rpm$//'
        fi
    ) | sort -u
}

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

    # Complete options for commands
    case "$prev" in
        install|add|i|a|fetch|f|download|get|pkgbuild|pb)
            COMPREPLY=( $(compgen -W "$(_haj_repo_pkgs)" -- "$cur") )
            return 0
            ;;
        remove|rm|toss|uninstall|delete|drop|mark|m|show|info|about|describe|files|lf)
            COMPREPLY=( $(compgen -W "$(_haj_installed_pkgs)" -- "$cur") )
            return 0
            ;;
        downgrade|sink|rollback)
            COMPREPLY=( $(compgen -W "$(_haj_downgrade_pkgs)" -- "$cur") )
            return 0
            ;;
        group|g)
            COMPREPLY=( $(compgen -W "$(_haj_groups)" -- "$cur") )
            return 0
            ;;
        load|l)
            COMPREPLY=( $(compgen -d -- "$cur"; compgen -f -- "$cur" | grep -E '\.(pkg\.tar\.(zst|xz|gz|bz2|lz4|lzo)|deb|rpm)$') )
            return 0
            ;;
        --from)
            COMPREPLY=( $(compgen -W "aur repo" -- "$cur") )
            return 0
            ;;
        -c|--config|--root)
            COMPREPLY=( $(compgen -f -- "$cur") )
            return 0
            ;;
        owns|ow)
            COMPREPLY=( $(compgen -f -- "$cur") )
            return 0
            ;;
        list|ls)
            COMPREPLY=( $(compgen -W "-e --explicit -p --deps -f --foreign" -- "$cur") )
            return 0
            ;;
        clean|c)
            COMPREPLY=( $(compgen -W "-k --keep" -- "$cur") )
            return 0
            ;;
        history|h)
            COMPREPLY=( $(compgen -W "-l --limit" -- "$cur") )
            return 0
            ;;
        upgrade|jump)
            COMPREPLY=( $(compgen -W "--no-sync" -- "$cur") )
            return 0
            ;;
    esac

    # Complete global options if they start with -
    if [[ "$cur" == -* ]]; then
        opts="-a --aur -r --repo --from -y --noconfirm -n --needed -i --ignore -c --config --root -v --verbose -d --dry-run -V --version -h --help"
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return 0
    fi

    # Complete commands
    if [ $COMP_CWORD -eq 1 ]; then
        opts="tui update refresh upgrade install add remove uninstall drop search find show about group list stats load fetch download downgrade sink rollback owns which files locate where history orphan orphans prune clean mark diff pacnew pkgbuild pb"
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return 0
    fi
}
complete -F _haj_completions haj
