#!/usr/bin/env bash
# Bash completion for the bunnify CLI (fzf-backed when available).
#
# Install (bash):
#   source /path/to/bunnify/etc/bunnify-completion
#
# Requires a running Bunnify server so `bunnify --list-keys` can enumerate shortcuts.
# Prefers fzf for interactive selection on Tab; falls back to COMPREPLY.

_bunnify_completion() {
    local cur
    local pending_option
    local expecting_option_value
    local first_positional_seen
    local i
    local reply
    local word
    local -a list_keys_cmd
    local -a options
    local -a color_values
    local -a edit_mode_values
    local keys
    local selected
    cur="${COMP_WORDS[COMP_CWORD]}"
    pending_option=""
    expecting_option_value=false
    first_positional_seen=false
    list_keys_cmd=(bunnify --list-keys)
    color_values=(auto always never)
    edit_mode_values=(emacs vim)

    if [[ "${cur}" == -* ]]; then
        options=(--base-url --complete-param --list-keys --list-usage --fzf --prefix --query --print-url --dry-run --color --edit-mode --env-file --help)
        COMPREPLY=()
        while IFS= read -r reply; do
            COMPREPLY+=("${reply}")
        done < <(compgen -W "${options[*]}" -- "${cur}")
        return 0
    fi

    for ((i = 1; i < COMP_CWORD; i++)); do
        word="${COMP_WORDS[i]}"
        if [[ "${expecting_option_value}" == true ]]; then
            if [[ "${pending_option}" == "--base-url" ]]; then
                list_keys_cmd+=(--base-url "${word}")
            elif [[ "${pending_option}" == "--env-file" ]]; then
                list_keys_cmd+=(--env-file "${word}")
            fi
            pending_option=""
            expecting_option_value=false
            continue
        fi
        case "${word}" in
            --base-url|--query|--color|--env-file|--edit-mode)
                pending_option="${word}"
                expecting_option_value=true
                ;;
            --base-url=*)
                list_keys_cmd+=("${word}")
                ;;
            --env-file=*)
                list_keys_cmd+=("${word}")
                ;;
            --query=*|--color=*|--edit-mode=*)
                ;;
            -*)
                pending_option=""
                ;;
            *)
                first_positional_seen=true
                ;;
        esac
        if [[ "${first_positional_seen}" == true ]]; then
            return 0
        fi
    done

    if [[ "${expecting_option_value}" == true ]]; then
        case "${pending_option}" in
            --color)
                COMPREPLY=()
                while IFS= read -r reply; do
                    COMPREPLY+=("${reply}")
                done < <(compgen -W "${color_values[*]}" -- "${cur}")
                return 0
                ;;
            --edit-mode)
                COMPREPLY=()
                while IFS= read -r reply; do
                    COMPREPLY+=("${reply}")
                done < <(compgen -W "${edit_mode_values[*]}" -- "${cur}")
                return 0
                ;;
            --env-file)
                COMPREPLY=()
                while IFS= read -r reply; do
                    COMPREPLY+=("${reply}")
                done < <(compgen -f -- "${cur}")
                return 0
                ;;
            *)
                return 0
                ;;
        esac
    fi

    # Detach stdin so resolve_base_url does not prompt (and persist) during Tab
    # completion when bunnify.env / BUNNIFY_BASE_URL are unset — stdin would
    # otherwise still be a TTY in the interactive shell.
    if ! keys="$("${list_keys_cmd[@]}" </dev/null 2>/dev/null)"; then
        return 0
    fi

    if command -v fzf >/dev/null 2>&1; then
        # Prefer fzf for interactive selection on Tab.
        selected="$(printf '%s\n' "${keys}" | fzf --query="${cur}" --select-1 --exit-0 --height=40% --layout=reverse --prompt='bunnify> ')"
        if [[ -n "${selected}" ]]; then
            COMPREPLY=( "${selected}" )
        fi
        return 0
    fi

    COMPREPLY=()
    while IFS= read -r reply; do
        COMPREPLY+=("${reply}")
    done < <(compgen -W "${keys}" -- "${cur}")
}

complete -F _bunnify_completion bunnify
