_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 batch-reboot fault-domain-update instance-list"
    local reboot_type_options="RESET SOFTRESET DIAGNOSTICREBOOT"
    local tenants=()
    local regions=()

    local instance_list_states=()
    local fault_domains=()
    if [[ -f "${script_path}" ]]; then
        tenants=( $(awk -F "[()']" '/_supported_tenants=/{for(i=3;i<NF-1;i+=2)print $i}' "${script_path}") )
        regions=( $(awk -F "[()']" '/_supported_regions=/{for(i=3;i<NF-1;i+=2)print $i}' "${script_path}") )
        instance_list_states=( $(awk -F "[()']" '/_instance_list_states=/{for(i=3;i<NF-1;i+=2)print $i}' "${script_path}") )
        fault_domains=( $(awk -F "[()']" '/_supported_fault_domains=/{for(i=3;i<NF-1;i+=2)print $i}' "${script_path}") )
    fi
    if [[ ${#instance_list_states[@]} -eq 0 ]]; then
        instance_list_states=(RUNNING STARTING STOPPED STOPPING)
    fi
    if [[ ${#fault_domains[@]} -eq 0 ]]; then
        fault_domains=(FAULT-DOMAIN-1 FAULT-DOMAIN-2 FAULT-DOMAIN-3)
    fi

    local opts="--mode --hostname --region --tenant --reboot-type --announcement-id --fault-domain --state --scheduled-terminate --exclude-scheduled-terminate --json --debug --dry-run -m -h -r -t -f -d -A"

    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
            ;;
        --announcement-id|-A)
            COMPREPLY=()
            return 0
            ;;
        --fault-domain|-f)
            COMPREPLY=( $(compgen -W "${fault_domains[*]}" -- "${cur}") )
            return 0
            ;;
        --state)
            COMPREPLY=( $(compgen -W "${instance_list_states[*]}" -- "${cur}") )
            return 0
            ;;
        *)
            COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
            ;;
    esac

    return 0
}

complete -F _oci_rescue_instance_completion oci_rescue_instance


