_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
    if [[ ${#tenants[@]} -eq 0 ]]; then
        tenants=(bhhosting bhpreprodhosting dsnhosting dsnpreprdhosting nfpreprodhosting lahosting lapreprodhosting nfinfra)
    fi
    if [[ ${#regions[@]} -eq 0 ]]; then
        regions=(eu-amsterdam-1 eu-stockholm-1 me-abudhabi-1 sa-bogota-1 ap-mumbai-1 eu-paris-1 uk-cardiff-1 me-dubai-1 eu-frankfurt-1 sa-saopaulo-1 ap-batam-1 ap-hyderabad-1 us-ashburn-1 ap-seoul-1 me-jeddah-1 af-johannesburg-1 ap-osaka-1 uk-london-1 eu-milan-1 eu-madrid-1 ap-melbourne-1 eu-marseille-1 mx-monterrey-1 il-jerusalem-1 ap-tokyo-1 us-chicago-1 us-phoenix-1 mx-queretaro-1 me-riyadh-1 sa-santiago-1 ap-singapore-1 us-sanjose-1 ap-sydney-1 sa-valparaiso-1 sa-vinhedo-1 ap-singapore-2 ap-chuncheon-1 ca-montreal-1 ca-toronto-1 eu-zurich-1)
    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


