// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Code generated by the Google Gen AI SDK generator DO NOT EDIT.

package genai

import (
	"context"
	"fmt"
	"iter"
	"net/http"
	"reflect"
)

func createCachedContentConfigToMldev(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromTtl := InternalGetValueByPath(fromObject, []string{"ttl"})
	if fromTtl != nil {
		InternalSetValueByPath(parentObject, []string{"ttl"}, fromTtl)
	}

	fromExpireTime := InternalGetValueByPath(fromObject, []string{"expireTime"})
	if fromExpireTime != nil {
		InternalSetValueByPath(parentObject, []string{"expireTime"}, fromExpireTime)
	}

	fromDisplayName := InternalGetValueByPath(fromObject, []string{"displayName"})
	if fromDisplayName != nil {
		InternalSetValueByPath(parentObject, []string{"displayName"}, fromDisplayName)
	}

	fromContents := InternalGetValueByPath(fromObject, []string{"contents"})
	if fromContents != nil {
		fromContents, err = InternalTContents(fromContents)
		if err != nil {
			return nil, err
		}

		fromContents, err = InternalApplyConverterToSliceWithRoot(fromContents.([]any), contentToMldev, rootObject)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(parentObject, []string{"contents"}, fromContents)
	}

	fromSystemInstruction := InternalGetValueByPath(fromObject, []string{"systemInstruction"})
	if fromSystemInstruction != nil {
		fromSystemInstruction, err = InternalTContent(fromSystemInstruction)
		if err != nil {
			return nil, err
		}

		fromSystemInstruction, err = contentToMldev(fromSystemInstruction.(map[string]any), toObject, rootObject)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(parentObject, []string{"systemInstruction"}, fromSystemInstruction)
	}

	fromTools := InternalGetValueByPath(fromObject, []string{"tools"})
	if fromTools != nil {
		fromTools, err = InternalApplyConverterToSliceWithRoot(fromTools.([]any), toolToMldev, rootObject)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(parentObject, []string{"tools"}, fromTools)
	}

	fromToolConfig := InternalGetValueByPath(fromObject, []string{"toolConfig"})
	if fromToolConfig != nil {
		fromToolConfig, err = toolConfigToMldev(fromToolConfig.(map[string]any), toObject, rootObject)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(parentObject, []string{"toolConfig"}, fromToolConfig)
	}

	if InternalGetValueByPath(fromObject, []string{"kmsKeyName"}) != nil {
		return nil, fmt.Errorf("kmsKeyName parameter is only supported in Gemini Enterprise Agent Platform mode, not in Gemini Developer API mode.")
	}

	return toObject, nil
}

func createCachedContentConfigToVertex(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromTtl := InternalGetValueByPath(fromObject, []string{"ttl"})
	if fromTtl != nil {
		InternalSetValueByPath(parentObject, []string{"ttl"}, fromTtl)
	}

	fromExpireTime := InternalGetValueByPath(fromObject, []string{"expireTime"})
	if fromExpireTime != nil {
		InternalSetValueByPath(parentObject, []string{"expireTime"}, fromExpireTime)
	}

	fromDisplayName := InternalGetValueByPath(fromObject, []string{"displayName"})
	if fromDisplayName != nil {
		InternalSetValueByPath(parentObject, []string{"displayName"}, fromDisplayName)
	}

	fromContents := InternalGetValueByPath(fromObject, []string{"contents"})
	if fromContents != nil {
		fromContents, err = InternalTContents(fromContents)
		if err != nil {
			return nil, err
		}

		fromContents, err = InternalApplyConverterToSliceWithRoot(fromContents.([]any), contentToVertex, rootObject)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(parentObject, []string{"contents"}, fromContents)
	}

	fromSystemInstruction := InternalGetValueByPath(fromObject, []string{"systemInstruction"})
	if fromSystemInstruction != nil {
		fromSystemInstruction, err = InternalTContent(fromSystemInstruction)
		if err != nil {
			return nil, err
		}

		fromSystemInstruction, err = contentToVertex(fromSystemInstruction.(map[string]any), toObject, rootObject)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(parentObject, []string{"systemInstruction"}, fromSystemInstruction)
	}

	fromTools := InternalGetValueByPath(fromObject, []string{"tools"})
	if fromTools != nil {
		fromTools, err = InternalApplyConverterToSliceWithRoot(fromTools.([]any), toolToVertex, rootObject)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(parentObject, []string{"tools"}, fromTools)
	}

	fromToolConfig := InternalGetValueByPath(fromObject, []string{"toolConfig"})
	if fromToolConfig != nil {
		fromToolConfig, err = toolConfigToVertex(fromToolConfig.(map[string]any), toObject, rootObject)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(parentObject, []string{"toolConfig"}, fromToolConfig)
	}

	fromKmsKeyName := InternalGetValueByPath(fromObject, []string{"kmsKeyName"})
	if fromKmsKeyName != nil {
		InternalSetValueByPath(parentObject, []string{"encryption_spec", "kmsKeyName"}, fromKmsKeyName)
	}

	return toObject, nil
}

func createCachedContentParametersToMldev(ac *InternalAPIClient, fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromModel := InternalGetValueByPath(fromObject, []string{"model"})
	if fromModel != nil {
		fromModel, err = InternalTCachesModel(ac, fromModel)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(toObject, []string{"model"}, fromModel)
	}

	fromConfig := InternalGetValueByPath(fromObject, []string{"config"})
	if fromConfig != nil {
		_, err = createCachedContentConfigToMldev(fromConfig.(map[string]any), toObject, rootObject)
		if err != nil {
			return nil, err
		}
	}

	return toObject, nil
}

func createCachedContentParametersToVertex(ac *InternalAPIClient, fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromModel := InternalGetValueByPath(fromObject, []string{"model"})
	if fromModel != nil {
		fromModel, err = InternalTCachesModel(ac, fromModel)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(toObject, []string{"model"}, fromModel)
	}

	fromConfig := InternalGetValueByPath(fromObject, []string{"config"})
	if fromConfig != nil {
		_, err = createCachedContentConfigToVertex(fromConfig.(map[string]any), toObject, rootObject)
		if err != nil {
			return nil, err
		}
	}

	return toObject, nil
}

func deleteCachedContentParametersToMldev(ac *InternalAPIClient, fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromName := InternalGetValueByPath(fromObject, []string{"name"})
	if fromName != nil {
		fromName, err = InternalTCachedContentName(ac, fromName)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(toObject, []string{"_url", "name"}, fromName)
	}

	return toObject, nil
}

func deleteCachedContentParametersToVertex(ac *InternalAPIClient, fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromName := InternalGetValueByPath(fromObject, []string{"name"})
	if fromName != nil {
		fromName, err = InternalTCachedContentName(ac, fromName)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(toObject, []string{"_url", "name"}, fromName)
	}

	return toObject, nil
}

func deleteCachedContentResponseFromMldev(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromSdkHttpResponse := InternalGetValueByPath(fromObject, []string{"sdkHttpResponse"})
	if fromSdkHttpResponse != nil {
		InternalSetValueByPath(toObject, []string{"sdkHttpResponse"}, fromSdkHttpResponse)
	}

	return toObject, nil
}

func deleteCachedContentResponseFromVertex(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromSdkHttpResponse := InternalGetValueByPath(fromObject, []string{"sdkHttpResponse"})
	if fromSdkHttpResponse != nil {
		InternalSetValueByPath(toObject, []string{"sdkHttpResponse"}, fromSdkHttpResponse)
	}

	return toObject, nil
}

func getCachedContentParametersToMldev(ac *InternalAPIClient, fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromName := InternalGetValueByPath(fromObject, []string{"name"})
	if fromName != nil {
		fromName, err = InternalTCachedContentName(ac, fromName)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(toObject, []string{"_url", "name"}, fromName)
	}

	return toObject, nil
}

func getCachedContentParametersToVertex(ac *InternalAPIClient, fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromName := InternalGetValueByPath(fromObject, []string{"name"})
	if fromName != nil {
		fromName, err = InternalTCachedContentName(ac, fromName)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(toObject, []string{"_url", "name"}, fromName)
	}

	return toObject, nil
}

func listCachedContentsConfigToMldev(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromPageSize := InternalGetValueByPath(fromObject, []string{"pageSize"})
	if fromPageSize != nil {
		InternalSetValueByPath(parentObject, []string{"_query", "pageSize"}, fromPageSize)
	}

	fromPageToken := InternalGetValueByPath(fromObject, []string{"pageToken"})
	if fromPageToken != nil {
		InternalSetValueByPath(parentObject, []string{"_query", "pageToken"}, fromPageToken)
	}

	return toObject, nil
}

func listCachedContentsConfigToVertex(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromPageSize := InternalGetValueByPath(fromObject, []string{"pageSize"})
	if fromPageSize != nil {
		InternalSetValueByPath(parentObject, []string{"_query", "pageSize"}, fromPageSize)
	}

	fromPageToken := InternalGetValueByPath(fromObject, []string{"pageToken"})
	if fromPageToken != nil {
		InternalSetValueByPath(parentObject, []string{"_query", "pageToken"}, fromPageToken)
	}

	return toObject, nil
}

func listCachedContentsParametersToMldev(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromConfig := InternalGetValueByPath(fromObject, []string{"config"})
	if fromConfig != nil {
		_, err = listCachedContentsConfigToMldev(fromConfig.(map[string]any), toObject, rootObject)
		if err != nil {
			return nil, err
		}
	}

	return toObject, nil
}

func listCachedContentsParametersToVertex(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromConfig := InternalGetValueByPath(fromObject, []string{"config"})
	if fromConfig != nil {
		_, err = listCachedContentsConfigToVertex(fromConfig.(map[string]any), toObject, rootObject)
		if err != nil {
			return nil, err
		}
	}

	return toObject, nil
}

func listCachedContentsResponseFromMldev(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromSdkHttpResponse := InternalGetValueByPath(fromObject, []string{"sdkHttpResponse"})
	if fromSdkHttpResponse != nil {
		InternalSetValueByPath(toObject, []string{"sdkHttpResponse"}, fromSdkHttpResponse)
	}

	fromNextPageToken := InternalGetValueByPath(fromObject, []string{"nextPageToken"})
	if fromNextPageToken != nil {
		InternalSetValueByPath(toObject, []string{"nextPageToken"}, fromNextPageToken)
	}

	fromCachedContents := InternalGetValueByPath(fromObject, []string{"cachedContents"})
	if fromCachedContents != nil {
		InternalSetValueByPath(toObject, []string{"cachedContents"}, fromCachedContents)
	}

	return toObject, nil
}

func listCachedContentsResponseFromVertex(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromSdkHttpResponse := InternalGetValueByPath(fromObject, []string{"sdkHttpResponse"})
	if fromSdkHttpResponse != nil {
		InternalSetValueByPath(toObject, []string{"sdkHttpResponse"}, fromSdkHttpResponse)
	}

	fromNextPageToken := InternalGetValueByPath(fromObject, []string{"nextPageToken"})
	if fromNextPageToken != nil {
		InternalSetValueByPath(toObject, []string{"nextPageToken"}, fromNextPageToken)
	}

	fromCachedContents := InternalGetValueByPath(fromObject, []string{"cachedContents"})
	if fromCachedContents != nil {
		InternalSetValueByPath(toObject, []string{"cachedContents"}, fromCachedContents)
	}

	return toObject, nil
}

func updateCachedContentConfigToMldev(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromTtl := InternalGetValueByPath(fromObject, []string{"ttl"})
	if fromTtl != nil {
		InternalSetValueByPath(parentObject, []string{"ttl"}, fromTtl)
	}

	fromExpireTime := InternalGetValueByPath(fromObject, []string{"expireTime"})
	if fromExpireTime != nil {
		InternalSetValueByPath(parentObject, []string{"expireTime"}, fromExpireTime)
	}

	return toObject, nil
}

func updateCachedContentConfigToVertex(fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromTtl := InternalGetValueByPath(fromObject, []string{"ttl"})
	if fromTtl != nil {
		InternalSetValueByPath(parentObject, []string{"ttl"}, fromTtl)
	}

	fromExpireTime := InternalGetValueByPath(fromObject, []string{"expireTime"})
	if fromExpireTime != nil {
		InternalSetValueByPath(parentObject, []string{"expireTime"}, fromExpireTime)
	}

	return toObject, nil
}

func updateCachedContentParametersToMldev(ac *InternalAPIClient, fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromName := InternalGetValueByPath(fromObject, []string{"name"})
	if fromName != nil {
		fromName, err = InternalTCachedContentName(ac, fromName)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(toObject, []string{"_url", "name"}, fromName)
	}

	fromConfig := InternalGetValueByPath(fromObject, []string{"config"})
	if fromConfig != nil {
		_, err = updateCachedContentConfigToMldev(fromConfig.(map[string]any), toObject, rootObject)
		if err != nil {
			return nil, err
		}
	}

	return toObject, nil
}

func updateCachedContentParametersToVertex(ac *InternalAPIClient, fromObject map[string]any, parentObject map[string]any, rootObject map[string]any) (toObject map[string]any, err error) {
	toObject = make(map[string]any)

	fromName := InternalGetValueByPath(fromObject, []string{"name"})
	if fromName != nil {
		fromName, err = InternalTCachedContentName(ac, fromName)
		if err != nil {
			return nil, err
		}

		InternalSetValueByPath(toObject, []string{"_url", "name"}, fromName)
	}

	fromConfig := InternalGetValueByPath(fromObject, []string{"config"})
	if fromConfig != nil {
		_, err = updateCachedContentConfigToVertex(fromConfig.(map[string]any), toObject, rootObject)
		if err != nil {
			return nil, err
		}
	}

	return toObject, nil
}

// Caches provides methods for managing the context caching.
// You don't need to initiate this struct. Create a client instance via NewClient, and
// then access Caches through client.Caches field.
type Caches struct {
	apiClient *InternalAPIClient
}

// Create creates a new cached content resource.
func (m Caches) Create(ctx context.Context, model string, config *CreateCachedContentConfig) (*CachedContent, error) {
	parameterMap := make(map[string]any)

	kwargs := map[string]any{"model": model, "config": config}
	InternalDeepMarshal(kwargs, &parameterMap)

	var httpOptions *HTTPOptions
	if config == nil || config.HTTPOptions == nil {
		httpOptions = &HTTPOptions{}
	} else {
		httpOptions = config.HTTPOptions
	}
	if httpOptions.Headers == nil {
		httpOptions.Headers = http.Header{}
	}
	var response = new(CachedContent)
	var responseMap map[string]any
	var toConverter func(*InternalAPIClient, map[string]any, map[string]any, map[string]any) (map[string]any, error)
	if m.apiClient.ClientConfig().Backend == BackendVertexAI {
		toConverter = createCachedContentParametersToVertex

	} else {
		toConverter = createCachedContentParametersToMldev

	}

	body, err := toConverter(m.apiClient, parameterMap, nil, parameterMap)
	if err != nil {
		return nil, err
	}

	var path string
	var urlParams map[string]any
	if _, ok := body["_url"]; ok {
		urlParams = body["_url"].(map[string]any)
		delete(body, "_url")
	}
	path, err = InternalFormatMap("cachedContents", urlParams)
	if err != nil {
		return nil, fmt.Errorf("invalid url params: %#v.\n%w", urlParams, err)
	}
	if _, ok := body["_query"]; ok {
		query, err := InternalCreateURLQuery(body["_query"].(map[string]any))
		if err != nil {
			return nil, err
		}
		path += "?" + query
		delete(body, "_query")
	}
	responseMap, err = sendRequest(ctx, m.apiClient, path, http.MethodPost, body, httpOptions)
	if err != nil {
		return nil, err
	}
	err = InternalMapToStruct(responseMap, response)
	if err != nil {
		return nil, err
	}

	if field, ok := reflect.TypeOf(response).Elem().FieldByName("SDKHTTPResponse"); ok {
		{
			if reflect.ValueOf(response).Elem().FieldByName("SDKHTTPResponse").IsValid() {
				{
					reflect.ValueOf(response).Elem().FieldByName("SDKHTTPResponse").Set(reflect.Zero(field.Type))
				}
			}
		}
	}

	return response, nil
}

// Get gets a cached content resource.
func (m Caches) Get(ctx context.Context, name string, config *GetCachedContentConfig) (*CachedContent, error) {
	parameterMap := make(map[string]any)

	kwargs := map[string]any{"name": name, "config": config}
	InternalDeepMarshal(kwargs, &parameterMap)

	var httpOptions *HTTPOptions
	if config == nil || config.HTTPOptions == nil {
		httpOptions = &HTTPOptions{}
	} else {
		httpOptions = config.HTTPOptions
	}
	if httpOptions.Headers == nil {
		httpOptions.Headers = http.Header{}
	}
	var response = new(CachedContent)
	var responseMap map[string]any
	var toConverter func(*InternalAPIClient, map[string]any, map[string]any, map[string]any) (map[string]any, error)
	if m.apiClient.ClientConfig().Backend == BackendVertexAI {
		toConverter = getCachedContentParametersToVertex

	} else {
		toConverter = getCachedContentParametersToMldev

	}

	body, err := toConverter(m.apiClient, parameterMap, nil, parameterMap)
	if err != nil {
		return nil, err
	}

	var path string
	var urlParams map[string]any
	if _, ok := body["_url"]; ok {
		urlParams = body["_url"].(map[string]any)
		delete(body, "_url")
	}
	path, err = InternalFormatMap("{name}", urlParams)
	if err != nil {
		return nil, fmt.Errorf("invalid url params: %#v.\n%w", urlParams, err)
	}
	if _, ok := body["_query"]; ok {
		query, err := InternalCreateURLQuery(body["_query"].(map[string]any))
		if err != nil {
			return nil, err
		}
		path += "?" + query
		delete(body, "_query")
	}
	responseMap, err = sendRequest(ctx, m.apiClient, path, http.MethodGet, body, httpOptions)
	if err != nil {
		return nil, err
	}
	err = InternalMapToStruct(responseMap, response)
	if err != nil {
		return nil, err
	}

	if field, ok := reflect.TypeOf(response).Elem().FieldByName("SDKHTTPResponse"); ok {
		{
			if reflect.ValueOf(response).Elem().FieldByName("SDKHTTPResponse").IsValid() {
				{
					reflect.ValueOf(response).Elem().FieldByName("SDKHTTPResponse").Set(reflect.Zero(field.Type))
				}
			}
		}
	}

	return response, nil
}

// Delete deletes a cached content resource.
func (m Caches) Delete(ctx context.Context, name string, config *DeleteCachedContentConfig) (*DeleteCachedContentResponse, error) {
	parameterMap := make(map[string]any)

	kwargs := map[string]any{"name": name, "config": config}
	InternalDeepMarshal(kwargs, &parameterMap)

	var httpOptions *HTTPOptions
	if config == nil || config.HTTPOptions == nil {
		httpOptions = &HTTPOptions{}
	} else {
		httpOptions = config.HTTPOptions
	}
	if httpOptions.Headers == nil {
		httpOptions.Headers = http.Header{}
	}
	var response = new(DeleteCachedContentResponse)
	var responseMap map[string]any
	var fromConverter func(map[string]any, map[string]any, map[string]any) (map[string]any, error)
	var toConverter func(*InternalAPIClient, map[string]any, map[string]any, map[string]any) (map[string]any, error)
	if m.apiClient.ClientConfig().Backend == BackendVertexAI {
		toConverter = deleteCachedContentParametersToVertex
		fromConverter = deleteCachedContentResponseFromVertex
	} else {
		toConverter = deleteCachedContentParametersToMldev
		fromConverter = deleteCachedContentResponseFromMldev
	}

	body, err := toConverter(m.apiClient, parameterMap, nil, parameterMap)
	if err != nil {
		return nil, err
	}

	var path string
	var urlParams map[string]any
	if _, ok := body["_url"]; ok {
		urlParams = body["_url"].(map[string]any)
		delete(body, "_url")
	}
	path, err = InternalFormatMap("{name}", urlParams)
	if err != nil {
		return nil, fmt.Errorf("invalid url params: %#v.\n%w", urlParams, err)
	}
	if _, ok := body["_query"]; ok {
		query, err := InternalCreateURLQuery(body["_query"].(map[string]any))
		if err != nil {
			return nil, err
		}
		path += "?" + query
		delete(body, "_query")
	}
	responseMap, err = sendRequest(ctx, m.apiClient, path, http.MethodDelete, body, httpOptions)
	if err != nil {
		return nil, err
	}
	if fromConverter != nil {
		responseMap, err = fromConverter(responseMap, nil, parameterMap)
	}
	if err != nil {
		return nil, err
	}
	err = InternalMapToStruct(responseMap, response)
	if err != nil {
		return nil, err
	}

	return response, nil
}

// Update updates a cached content resource.
func (m Caches) Update(ctx context.Context, name string, config *UpdateCachedContentConfig) (*CachedContent, error) {
	parameterMap := make(map[string]any)

	kwargs := map[string]any{"name": name, "config": config}
	InternalDeepMarshal(kwargs, &parameterMap)

	var httpOptions *HTTPOptions
	if config == nil || config.HTTPOptions == nil {
		httpOptions = &HTTPOptions{}
	} else {
		httpOptions = config.HTTPOptions
	}
	if httpOptions.Headers == nil {
		httpOptions.Headers = http.Header{}
	}
	var response = new(CachedContent)
	var responseMap map[string]any
	var toConverter func(*InternalAPIClient, map[string]any, map[string]any, map[string]any) (map[string]any, error)
	if m.apiClient.ClientConfig().Backend == BackendVertexAI {
		toConverter = updateCachedContentParametersToVertex

	} else {
		toConverter = updateCachedContentParametersToMldev

	}

	body, err := toConverter(m.apiClient, parameterMap, nil, parameterMap)
	if err != nil {
		return nil, err
	}

	var path string
	var urlParams map[string]any
	if _, ok := body["_url"]; ok {
		urlParams = body["_url"].(map[string]any)
		delete(body, "_url")
	}
	path, err = InternalFormatMap("{name}", urlParams)
	if err != nil {
		return nil, fmt.Errorf("invalid url params: %#v.\n%w", urlParams, err)
	}
	if _, ok := body["_query"]; ok {
		query, err := InternalCreateURLQuery(body["_query"].(map[string]any))
		if err != nil {
			return nil, err
		}
		path += "?" + query
		delete(body, "_query")
	}
	responseMap, err = sendRequest(ctx, m.apiClient, path, http.MethodPatch, body, httpOptions)
	if err != nil {
		return nil, err
	}
	err = InternalMapToStruct(responseMap, response)
	if err != nil {
		return nil, err
	}

	if field, ok := reflect.TypeOf(response).Elem().FieldByName("SDKHTTPResponse"); ok {
		{
			if reflect.ValueOf(response).Elem().FieldByName("SDKHTTPResponse").IsValid() {
				{
					reflect.ValueOf(response).Elem().FieldByName("SDKHTTPResponse").Set(reflect.Zero(field.Type))
				}
			}
		}
	}

	return response, nil
}

func (m Caches) list(ctx context.Context, config *ListCachedContentsConfig) (*ListCachedContentsResponse, error) {
	parameterMap := make(map[string]any)

	kwargs := map[string]any{"config": config}
	InternalDeepMarshal(kwargs, &parameterMap)

	var httpOptions *HTTPOptions
	if config == nil || config.HTTPOptions == nil {
		httpOptions = &HTTPOptions{}
	} else {
		httpOptions = config.HTTPOptions
	}
	if httpOptions.Headers == nil {
		httpOptions.Headers = http.Header{}
	}
	var response = new(ListCachedContentsResponse)
	var responseMap map[string]any
	var fromConverter func(map[string]any, map[string]any, map[string]any) (map[string]any, error)
	var toConverter func(map[string]any, map[string]any, map[string]any) (map[string]any, error)
	if m.apiClient.ClientConfig().Backend == BackendVertexAI {
		toConverter = listCachedContentsParametersToVertex
		fromConverter = listCachedContentsResponseFromVertex
	} else {
		toConverter = listCachedContentsParametersToMldev
		fromConverter = listCachedContentsResponseFromMldev
	}

	body, err := toConverter(parameterMap, nil, parameterMap)
	if err != nil {
		return nil, err
	}

	var path string
	var urlParams map[string]any
	if _, ok := body["_url"]; ok {
		urlParams = body["_url"].(map[string]any)
		delete(body, "_url")
	}
	path, err = InternalFormatMap("cachedContents", urlParams)
	if err != nil {
		return nil, fmt.Errorf("invalid url params: %#v.\n%w", urlParams, err)
	}
	if _, ok := body["_query"]; ok {
		query, err := InternalCreateURLQuery(body["_query"].(map[string]any))
		if err != nil {
			return nil, err
		}
		path += "?" + query
		delete(body, "_query")
	}
	responseMap, err = sendRequest(ctx, m.apiClient, path, http.MethodGet, body, httpOptions)
	if err != nil {
		return nil, err
	}
	if fromConverter != nil {
		responseMap, err = fromConverter(responseMap, nil, parameterMap)
	}
	if err != nil {
		return nil, err
	}
	err = InternalMapToStruct(responseMap, response)
	if err != nil {
		return nil, err
	}

	return response, nil
}

// List retrieves a paginated list of cached_contents resources.
func (m Caches) List(ctx context.Context, config *ListCachedContentsConfig) (Page[CachedContent], error) {
	listFunc := func(ctx context.Context, config map[string]any) ([]*CachedContent, string, *HTTPResponse, error) {
		var c ListCachedContentsConfig
		if err := InternalMapToStruct(config, &c); err != nil {
			return nil, "", nil, err
		}
		resp, err := m.list(ctx, &c)
		if err != nil {
			return nil, "", nil, err
		}
		return resp.CachedContents, resp.NextPageToken, resp.SDKHTTPResponse, nil
	}
	c := make(map[string]any)
	InternalDeepMarshal(config, &c)
	return newPage(ctx, "cachedContents", c, listFunc)
}

// All retrieves all cached_contents resources.
//
// This method handles pagination internally, making multiple API calls as needed
// to fetch all entries. It returns an iterator that yields each "cachedContents"
// entry one by one. You do not need to manage pagination
// tokens or make multiple calls to retrieve all data.
func (m Caches) All(ctx context.Context) iter.Seq2[*CachedContent, error] {
	listFunc := func(ctx context.Context, config map[string]any) ([]*CachedContent, string, *HTTPResponse, error) {
		var c ListCachedContentsConfig
		if err := InternalMapToStruct(config, &c); err != nil {
			return nil, "", nil, err
		}
		resp, err := m.list(ctx, &c)
		if err != nil {
			return nil, "", nil, err
		}
		return resp.CachedContents, resp.NextPageToken, resp.SDKHTTPResponse, nil
	}
	p, err := newPage(ctx, "cachedContents", map[string]any{}, listFunc)
	if err != nil {
		return yieldErrorAndEndIterator[CachedContent](err)
	}
	return p.all(ctx)
}
