_oci_rescue_instance_completion() {
    local cur prev words cword
    _get_comp_words_by_ref -n "$PROG" cur prev words cword

    local script_path=$(command -v oci_rescue_instance)

    local modes=("rescue" "unrescue")
    local tenants=()
    local regions=()

    if [[ -f "${script_path}" ]]; then
        tenants=( $(grep "_supported_tenants=" "${script_path}" | awk -F'[()]' '{print $2}' | tr -d "'" | tr ' ' '
' | sort -u | tr '
' ' ') )
        regions=( $(grep "_supported_regions=" "${script_path}" | awk -F'[()]' '{print $2}' | tr -d "'" | tr ' ' '
' | sort -u | tr '
' ' ') )
    fi

    local opts="--mode --hostname --region --tenant --debug -m -h -r -t -d"

    case "${prev}" in
        --mode|-m)
            COMPREPLY=( $(compgen -W "${modes[*]}" -- "${cur}") )
            ;;
        --tenant|-t)
            COMPREPLY=( $(compgen -W "${tenants[*]}" -- "${cur}") )
            ;;
        --region|-r)
            COMPREPLY=( $(compgen -W "${regions[*]}" -- "${cur}") )
            ;;
        --hostname|-h)
            # No suggestions for hostname, as it's an arbitrary value
            COMPREPLY=()
            ;;
        *)
            # Suggest options if the current word is empty (just hit tab after command)
            # or if it starts with '-', indicating a new option
            if [[ -z "${cur}" || "${cur}" == -* ]]; then
                COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
            else
                # Otherwise, it's likely a value for a previous argument, so no suggestions
                COMPREPLY=()
            fi
            ;;
    esac

    return 0
}

complete -F _oci_rescue_instance_completion oci_rescue_instance


