/*
Merlin

API Guide for accessing Merlin's model management, deployment, and serving functionalities

API version: 0.14.0
*/

// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.

package client

import (
	"bytes"
	"context"
	"io"
	"net/http"
	"net/url"
	"strings"
)

// VersionImageAPIService VersionImageAPI service
type VersionImageAPIService service

type ApiModelsModelIdVersionsVersionIdImageGetRequest struct {
	ctx        context.Context
	ApiService *VersionImageAPIService
	modelId    int32
	versionId  int32
}

func (r ApiModelsModelIdVersionsVersionIdImageGetRequest) Execute() (*VersionImage, *http.Response, error) {
	return r.ApiService.ModelsModelIdVersionsVersionIdImageGetExecute(r)
}

/*
ModelsModelIdVersionsVersionIdImageGet Get the Docker image for given model version

	@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
	@param modelId
	@param versionId
	@return ApiModelsModelIdVersionsVersionIdImageGetRequest
*/
func (a *VersionImageAPIService) ModelsModelIdVersionsVersionIdImageGet(ctx context.Context, modelId int32, versionId int32) ApiModelsModelIdVersionsVersionIdImageGetRequest {
	return ApiModelsModelIdVersionsVersionIdImageGetRequest{
		ApiService: a,
		ctx:        ctx,
		modelId:    modelId,
		versionId:  versionId,
	}
}

// Execute executes the request
//
//	@return VersionImage
func (a *VersionImageAPIService) ModelsModelIdVersionsVersionIdImageGetExecute(r ApiModelsModelIdVersionsVersionIdImageGetRequest) (*VersionImage, *http.Response, error) {
	var (
		localVarHTTPMethod  = http.MethodGet
		localVarPostBody    interface{}
		formFiles           []formFile
		localVarReturnValue *VersionImage
	)

	localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "VersionImageAPIService.ModelsModelIdVersionsVersionIdImageGet")
	if err != nil {
		return localVarReturnValue, nil, &GenericOpenAPIError{error: err.Error()}
	}

	localVarPath := localBasePath + "/models/{model_id}/versions/{version_id}/image"
	localVarPath = strings.Replace(localVarPath, "{"+"model_id"+"}", url.PathEscape(parameterValueToString(r.modelId, "modelId")), -1)
	localVarPath = strings.Replace(localVarPath, "{"+"version_id"+"}", url.PathEscape(parameterValueToString(r.versionId, "versionId")), -1)

	localVarHeaderParams := make(map[string]string)
	localVarQueryParams := url.Values{}
	localVarFormParams := url.Values{}

	// to determine the Content-Type header
	localVarHTTPContentTypes := []string{}

	// set Content-Type header
	localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes)
	if localVarHTTPContentType != "" {
		localVarHeaderParams["Content-Type"] = localVarHTTPContentType
	}

	// to determine the Accept header
	localVarHTTPHeaderAccepts := []string{"*/*"}

	// set Accept header
	localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
	if localVarHTTPHeaderAccept != "" {
		localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
	}
	if r.ctx != nil {
		// API Key Authentication
		if auth, ok := r.ctx.Value(ContextAPIKeys).(map[string]APIKey); ok {
			if apiKey, ok := auth["Bearer"]; ok {
				var key string
				if apiKey.Prefix != "" {
					key = apiKey.Prefix + " " + apiKey.Key
				} else {
					key = apiKey.Key
				}
				localVarHeaderParams["Authorization"] = key
			}
		}
	}
	req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
	if err != nil {
		return localVarReturnValue, nil, err
	}

	localVarHTTPResponse, err := a.client.callAPI(req)
	if err != nil || localVarHTTPResponse == nil {
		return localVarReturnValue, localVarHTTPResponse, err
	}

	localVarBody, err := io.ReadAll(localVarHTTPResponse.Body)
	localVarHTTPResponse.Body.Close()
	localVarHTTPResponse.Body = io.NopCloser(bytes.NewBuffer(localVarBody))
	if err != nil {
		return localVarReturnValue, localVarHTTPResponse, err
	}

	if localVarHTTPResponse.StatusCode >= 300 {
		newErr := &GenericOpenAPIError{
			body:  localVarBody,
			error: localVarHTTPResponse.Status,
		}
		return localVarReturnValue, localVarHTTPResponse, newErr
	}

	err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
	if err != nil {
		newErr := &GenericOpenAPIError{
			body:  localVarBody,
			error: err.Error(),
		}
		return localVarReturnValue, localVarHTTPResponse, newErr
	}

	return localVarReturnValue, localVarHTTPResponse, nil
}

type ApiModelsModelIdVersionsVersionIdImagePutRequest struct {
	ctx        context.Context
	ApiService *VersionImageAPIService
	modelId    int32
	versionId  int32
	body       *BuildImageOptions
}

func (r ApiModelsModelIdVersionsVersionIdImagePutRequest) Body(body BuildImageOptions) ApiModelsModelIdVersionsVersionIdImagePutRequest {
	r.body = &body
	return r
}

func (r ApiModelsModelIdVersionsVersionIdImagePutRequest) Execute() (*http.Response, error) {
	return r.ApiService.ModelsModelIdVersionsVersionIdImagePutExecute(r)
}

/*
ModelsModelIdVersionsVersionIdImagePut Build the Docker image for given model version

	@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
	@param modelId
	@param versionId
	@return ApiModelsModelIdVersionsVersionIdImagePutRequest
*/
func (a *VersionImageAPIService) ModelsModelIdVersionsVersionIdImagePut(ctx context.Context, modelId int32, versionId int32) ApiModelsModelIdVersionsVersionIdImagePutRequest {
	return ApiModelsModelIdVersionsVersionIdImagePutRequest{
		ApiService: a,
		ctx:        ctx,
		modelId:    modelId,
		versionId:  versionId,
	}
}

// Execute executes the request
func (a *VersionImageAPIService) ModelsModelIdVersionsVersionIdImagePutExecute(r ApiModelsModelIdVersionsVersionIdImagePutRequest) (*http.Response, error) {
	var (
		localVarHTTPMethod = http.MethodPut
		localVarPostBody   interface{}
		formFiles          []formFile
	)

	localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "VersionImageAPIService.ModelsModelIdVersionsVersionIdImagePut")
	if err != nil {
		return nil, &GenericOpenAPIError{error: err.Error()}
	}

	localVarPath := localBasePath + "/models/{model_id}/versions/{version_id}/image"
	localVarPath = strings.Replace(localVarPath, "{"+"model_id"+"}", url.PathEscape(parameterValueToString(r.modelId, "modelId")), -1)
	localVarPath = strings.Replace(localVarPath, "{"+"version_id"+"}", url.PathEscape(parameterValueToString(r.versionId, "versionId")), -1)

	localVarHeaderParams := make(map[string]string)
	localVarQueryParams := url.Values{}
	localVarFormParams := url.Values{}

	// to determine the Content-Type header
	localVarHTTPContentTypes := []string{}

	// set Content-Type header
	localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes)
	if localVarHTTPContentType != "" {
		localVarHeaderParams["Content-Type"] = localVarHTTPContentType
	}

	// to determine the Accept header
	localVarHTTPHeaderAccepts := []string{}

	// set Accept header
	localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
	if localVarHTTPHeaderAccept != "" {
		localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
	}
	// body params
	localVarPostBody = r.body
	if r.ctx != nil {
		// API Key Authentication
		if auth, ok := r.ctx.Value(ContextAPIKeys).(map[string]APIKey); ok {
			if apiKey, ok := auth["Bearer"]; ok {
				var key string
				if apiKey.Prefix != "" {
					key = apiKey.Prefix + " " + apiKey.Key
				} else {
					key = apiKey.Key
				}
				localVarHeaderParams["Authorization"] = key
			}
		}
	}
	req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
	if err != nil {
		return nil, err
	}

	localVarHTTPResponse, err := a.client.callAPI(req)
	if err != nil || localVarHTTPResponse == nil {
		return localVarHTTPResponse, err
	}

	localVarBody, err := io.ReadAll(localVarHTTPResponse.Body)
	localVarHTTPResponse.Body.Close()
	localVarHTTPResponse.Body = io.NopCloser(bytes.NewBuffer(localVarBody))
	if err != nil {
		return localVarHTTPResponse, err
	}

	if localVarHTTPResponse.StatusCode >= 300 {
		newErr := &GenericOpenAPIError{
			body:  localVarBody,
			error: localVarHTTPResponse.Status,
		}
		return localVarHTTPResponse, newErr
	}

	return localVarHTTPResponse, nil
}
