#!/usr/bin/env bash
# Bash completion script for arc package manager
# Install to: /etc/bash_completion.d/arc or /usr/share/bash-completion/completions/arc

_arc_completion() {
    local cur prev words cword
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    words=("${COMP_WORDS[@]}")
    cword=$COMP_CWORD

    # List of main commands
    local commands="search install uninstall remove info list config"
    
    # Handle global flags
    local global_flags="--verbose -v --help -h --version"

    # If we're completing the first argument (the command itself)
    if [[ $cword -eq 1 ]]; then
        if [[ "$cur" == -* ]]; then
            COMPREPLY=($(compgen -W "$global_flags" -- "$cur"))
        else
            COMPREPLY=($(compgen -W "$commands" -- "$cur"))
        fi
        return 0
    fi

    # Get the command being used
    local cmd=""
    for word in "${words[@]:1}"; do
        if [[ ! "$word" == -* ]]; then
            cmd="$word"
            break
        fi
    done

    # Complete based on the command
    case "$cmd" in
        search)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=($(compgen -W "--pacman-only --flatpak-only --verbose -v --help -h" -- "$cur"))
            fi
            ;;
        install)
            # Try to offer package name suggestions if we have internet
            if [[ "$cur" != -* ]]; then
                # Get installed packages and recently searched packages as suggestions
                local suggestions=""
                if command -v arc &>/dev/null; then
                    suggestions=$(arc list 2>/dev/null | grep -oE '(pacman|flatpak):[^ ]+' | cut -d: -f2 | sort -u)
                fi
                COMPREPLY=($(compgen -W "$suggestions" -- "$cur"))
            fi
            ;;
        uninstall|remove)
            # Complete with installed packages
            if [[ "$cur" != -* ]]; then
                local suggestions=""
                if command -v arc &>/dev/null; then
                    suggestions=$(arc list 2>/dev/null | grep -oE '(pacman|flatpak):[^ ]+' | cut -d: -f2 | sort -u)
                fi
                COMPREPLY=($(compgen -W "$suggestions" -- "$cur"))
            else
                COMPREPLY=($(compgen -W "--force -f --verbose -v --help -h" -- "$cur"))
            fi
            ;;
        info)
            # Complete with installed packages
            if [[ "$cur" != -* ]]; then
                local suggestions=""
                if command -v arc &>/dev/null; then
                    suggestions=$(arc list 2>/dev/null | grep -oE '(pacman|flatpak):[^ ]+' | cut -d: -f2 | sort -u)
                fi
                COMPREPLY=($(compgen -W "$suggestions" -- "$cur"))
            fi
            ;;
        list)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=($(compgen -W "--pacman-only --flatpak-only --verbose -v --help -h" -- "$cur"))
            fi
            ;;
        config)
            if [[ "$cur" == -* ]]; then
                COMPREPLY=($(compgen -W "--all -a --verbose -v --help -h" -- "$cur"))
            fi
            ;;
    esac

    return 0
}

# Register the completion function
complete -o bashdefault -o default -o nospace -F _arc_completion arc
