# shellcheck shell=dash disable=SC2034

xrc cfgy
___x_cmd_gemini_cfg(){
    [ "$#" -gt 0 ] || {
        ___x_cmd_gemini_init
        return
    }

    local op="$1"; shift
    case "$op" in
        -h|--help)      ___x_cmd help -m gemini --cfg ;;
        apikey=*)       ___x_cmd_gemini_cfg___invoke "$op" "$@"
                        ! ___x_cmd_gemini_has_apikey 2>/dev/null >&2 || ___x_cmd chat --setalias gemini gemini
                        ;;
        *)              ___x_cmd_gemini_cfg___invoke "$op" "$@" ;;
    esac
}

___x_cmd_gemini_cur(){
    local X_help_cmd=; X_help_cmd='x help -m gemini --cur' help:arg:parse
    ___x_cmd_gemini_cfg --current "$@"
}

___x_cmd_gemini_init(){
    local cur_apikey=; local cur_model=; local cur_endpoint=; local cur_temperature=
    local cur_proxy=;
    if [ -f "$(___x_cmd_gemini_cur --get config)" ]; then
        ___x_cmd_gemini_cur cur_apikey:=apikey cur_model:=model cur_endpoint:=endpoint cur_proxy:=proxy cur_temperature:=temperature
    fi

    gemini:info "To get Gemini's API key -> https://aistudio.google.com/app/api-keys"

    ___x_cmd_gemini_cfg___invoke --init ${cur_apikey:+"--ctrl_exit_strategy"}                            \
        apikey      "Set up the API key of Gemini key"      "$cur_apikey"                                \
                        '=~*'   "^[A-Za-z0-9-]+$"           --                                           \
        model       "Set the default model for Gemini AI"   "${cur_model:-"$___X_CMD_GEMINI_DEFAULT_FIRST_MODEL"}"           \
                        '='     gemini-2.5-pro gemini-2.5-flash gemini-2.0-flash            \
                                gemini-2.0-flash-lite    --                   \
        temperature "Set temperature (optional)"        "${cur_temperature:-0.3}"   --                   \
        endpoint    "Set endpoint (optional)"           "$cur_endpoint"             --                   \
        proxy       "Set up the network proxy for API requests (optional)"                               \
                    "$cur_proxy" '=~'    "^([a-z0-9]+://)?[0-9.:]+$" || return $?

    ! ___x_cmd_gemini_has_apikey 2>/dev/null >&2 || ___x_cmd chat --setalias gemini gemini
}

___x_cmd_gemini_has_apikey(){
    local apikey=
    ___x_cmd_gemini_cur     apikey:= 2>/dev/null
    [ -n "$apikey" ] || {
        gemini:error "Please setting up your apikey first ==> 'x gemini --cfg apikey=<your apikey>'"
        gemini:warn "You can get the gemini apikey from ==> https://aistudio.google.com/app/api-keys"
        return 1
    }
}

# Config setting the key.
# set the default prompt

___X_CMD_GEMINI_CFG_VARLIST="apikey,model,endpoint,proxy,temperature"
___x_cmd_gemini_cfg___invoke(){
    ___x_cmd_cfgy_obj                                               \
        --prefix            ___X_CMD_GEMINI_CFG_DATA                \
        --default-config    "${___X_CMD_ROOT_CFG}/gemini/X.cfg.yml" \
        --current-config    "${___X_CMD_GEMINI_LOCAL_CONFIG}"       \
        --current-profile   "${___X_CMD_GEMINI_LOCAL_PROFILE}"      \
        --varlist           "$___X_CMD_GEMINI_CFG_VARLIST"          \
        "$@"
}

