_oci_rescue_instance_completion() {
    local cur prev words cword
    _get_comp_words_by_ref cur prev words cword

    local script_path
    script_path=$(command -v oci_rescue_instance)

    local modes="rescue unrescue reboot"
    local reboot_type_options="RESET SOFTRESET"
    local tenants=()
    local regions=()

    if [[ -f "${script_path}" ]]; then
        tenants=( $(grep "_supported_tenants=" "${script_path}" | cut -d'(' -f2 | cut -d')' -f1 | tr -d "'") )
        regions=( $(grep "_supported_regions=" "${script_path}" | cut -d'(' -f2 | cut -d')' -f1 | tr -d "'") )
    fi

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

    case "${prev}" in
        --mode|-m)
            COMPREPLY=( $(compgen -W "${modes}" -- "${cur}") )
            return 0
            ;;
        --tenant|-t)
            COMPREPLY=( $(compgen -W "${tenants[*]}" -- "${cur}") )
            return 0
            ;;
        --region|-r)
            COMPREPLY=( $(compgen -W "${regions[*]}" -- "${cur}") )
            return 0
            ;;
        --hostname|-h)
            COMPREPLY=()
            return 0
            ;;
        --reboot-type)
            COMPREPLY=( $(compgen -W "${reboot_type_options}" -- "${cur}") )
            return 0
            ;;
        *)
            COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
            ;;
    esac

    return 0
}

complete -F _oci_rescue_instance_completion oci_rescue_instance


