// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// Code generated by Microsoft (R) Go Code Generator. DO NOT EDIT.

package armagricultureplatform

import (
	"context"
	"errors"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
	"net/http"
	"net/url"
	"strings"
)

// AgriServiceClient contains the methods for the AgriService group.
// Don't use this type directly, use NewAgriServiceClient() instead.
//
// Generated from API version 2024-06-01-preview
type AgriServiceClient struct {
	internal       *arm.Client
	subscriptionID string
}

// NewAgriServiceClient creates a new instance of AgriServiceClient with the specified values.
//   - subscriptionID - The ID of the target subscription. The value must be an UUID.
//   - credential - used to authorize requests. Usually a credential from azidentity.
//   - options - Contains optional client configuration. Pass nil to accept the default values.
func NewAgriServiceClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*AgriServiceClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &AgriServiceClient{
		subscriptionID: subscriptionID,
		internal:       cl,
	}
	return client, nil
}

// BeginCreateOrUpdate - Create a AgriServiceResource
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - agriServiceResourceName - The name of the AgriService resource.
//   - resource - Resource create parameters.
//   - options - AgriServiceClientBeginCreateOrUpdateOptions contains the optional parameters for the AgriServiceClient.BeginCreateOrUpdate
//     method.
func (client *AgriServiceClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, agriServiceResourceName string, resource AgriServiceResource, options *AgriServiceClientBeginCreateOrUpdateOptions) (*runtime.Poller[AgriServiceClientCreateOrUpdateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.createOrUpdate(ctx, resourceGroupName, agriServiceResourceName, resource, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[AgriServiceClientCreateOrUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[AgriServiceClientCreateOrUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// CreateOrUpdate - Create a AgriServiceResource
// If the operation fails it returns an *azcore.ResponseError type.
func (client *AgriServiceClient) createOrUpdate(ctx context.Context, resourceGroupName string, agriServiceResourceName string, resource AgriServiceResource, options *AgriServiceClientBeginCreateOrUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "AgriServiceClient.BeginCreateOrUpdate"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, agriServiceResourceName, resource, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// createOrUpdateCreateRequest creates the CreateOrUpdate request.
func (client *AgriServiceClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, agriServiceResourceName string, resource AgriServiceResource, _ *AgriServiceClientBeginCreateOrUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AgriculturePlatform/agriServices/{agriServiceResourceName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if agriServiceResourceName == "" {
		return nil, errors.New("parameter agriServiceResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{agriServiceResourceName}", url.PathEscape(agriServiceResourceName))
	req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20240601Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	req.Raw().Header["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, resource); err != nil {
		return nil, err
	}
	return req, nil
}

// BeginDelete - Delete a AgriServiceResource
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - agriServiceResourceName - The name of the AgriService resource.
//   - options - AgriServiceClientBeginDeleteOptions contains the optional parameters for the AgriServiceClient.BeginDelete method.
func (client *AgriServiceClient) BeginDelete(ctx context.Context, resourceGroupName string, agriServiceResourceName string, options *AgriServiceClientBeginDeleteOptions) (*runtime.Poller[AgriServiceClientDeleteResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.deleteOperation(ctx, resourceGroupName, agriServiceResourceName, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[AgriServiceClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[AgriServiceClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Delete - Delete a AgriServiceResource
// If the operation fails it returns an *azcore.ResponseError type.
func (client *AgriServiceClient) deleteOperation(ctx context.Context, resourceGroupName string, agriServiceResourceName string, options *AgriServiceClientBeginDeleteOptions) (*http.Response, error) {
	var err error
	const operationName = "AgriServiceClient.BeginDelete"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.deleteCreateRequest(ctx, resourceGroupName, agriServiceResourceName, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusAccepted, http.StatusNoContent) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// deleteCreateRequest creates the Delete request.
func (client *AgriServiceClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, agriServiceResourceName string, _ *AgriServiceClientBeginDeleteOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AgriculturePlatform/agriServices/{agriServiceResourceName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if agriServiceResourceName == "" {
		return nil, errors.New("parameter agriServiceResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{agriServiceResourceName}", url.PathEscape(agriServiceResourceName))
	req, err := runtime.NewRequest(ctx, http.MethodDelete, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20240601Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	return req, nil
}

// Get - Get a AgriServiceResource
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - agriServiceResourceName - The name of the AgriService resource.
//   - options - AgriServiceClientGetOptions contains the optional parameters for the AgriServiceClient.Get method.
func (client *AgriServiceClient) Get(ctx context.Context, resourceGroupName string, agriServiceResourceName string, options *AgriServiceClientGetOptions) (AgriServiceClientGetResponse, error) {
	var err error
	const operationName = "AgriServiceClient.Get"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.getCreateRequest(ctx, resourceGroupName, agriServiceResourceName, options)
	if err != nil {
		return AgriServiceClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return AgriServiceClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return AgriServiceClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *AgriServiceClient) getCreateRequest(ctx context.Context, resourceGroupName string, agriServiceResourceName string, _ *AgriServiceClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AgriculturePlatform/agriServices/{agriServiceResourceName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if agriServiceResourceName == "" {
		return nil, errors.New("parameter agriServiceResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{agriServiceResourceName}", url.PathEscape(agriServiceResourceName))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20240601Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getHandleResponse handles the Get response.
func (client *AgriServiceClient) getHandleResponse(resp *http.Response) (AgriServiceClientGetResponse, error) {
	result := AgriServiceClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.AgriServiceResource); err != nil {
		return AgriServiceClientGetResponse{}, err
	}
	return result, nil
}

// ListAvailableSolutions - Returns the list of available agri solutions.
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - agriServiceResourceName - The name of the AgriService resource.
//   - options - AgriServiceClientListAvailableSolutionsOptions contains the optional parameters for the AgriServiceClient.ListAvailableSolutions
//     method.
func (client *AgriServiceClient) ListAvailableSolutions(ctx context.Context, resourceGroupName string, agriServiceResourceName string, options *AgriServiceClientListAvailableSolutionsOptions) (AgriServiceClientListAvailableSolutionsResponse, error) {
	var err error
	const operationName = "AgriServiceClient.ListAvailableSolutions"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.listAvailableSolutionsCreateRequest(ctx, resourceGroupName, agriServiceResourceName, options)
	if err != nil {
		return AgriServiceClientListAvailableSolutionsResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return AgriServiceClientListAvailableSolutionsResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return AgriServiceClientListAvailableSolutionsResponse{}, err
	}
	resp, err := client.listAvailableSolutionsHandleResponse(httpResp)
	return resp, err
}

// listAvailableSolutionsCreateRequest creates the ListAvailableSolutions request.
func (client *AgriServiceClient) listAvailableSolutionsCreateRequest(ctx context.Context, resourceGroupName string, agriServiceResourceName string, _ *AgriServiceClientListAvailableSolutionsOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AgriculturePlatform/agriServices/{agriServiceResourceName}/listAvailableSolutions"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if agriServiceResourceName == "" {
		return nil, errors.New("parameter agriServiceResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{agriServiceResourceName}", url.PathEscape(agriServiceResourceName))
	req, err := runtime.NewRequest(ctx, http.MethodPost, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20240601Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listAvailableSolutionsHandleResponse handles the ListAvailableSolutions response.
func (client *AgriServiceClient) listAvailableSolutionsHandleResponse(resp *http.Response) (AgriServiceClientListAvailableSolutionsResponse, error) {
	result := AgriServiceClientListAvailableSolutionsResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.AvailableAgriSolutionListResult); err != nil {
		return AgriServiceClientListAvailableSolutionsResponse{}, err
	}
	return result, nil
}

// NewListByResourceGroupPager - List AgriServiceResource resources by resource group
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - options - AgriServiceClientListByResourceGroupOptions contains the optional parameters for the AgriServiceClient.NewListByResourceGroupPager
//     method.
func (client *AgriServiceClient) NewListByResourceGroupPager(resourceGroupName string, options *AgriServiceClientListByResourceGroupOptions) *runtime.Pager[AgriServiceClientListByResourceGroupResponse] {
	return runtime.NewPager(runtime.PagingHandler[AgriServiceClientListByResourceGroupResponse]{
		More: func(page AgriServiceClientListByResourceGroupResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *AgriServiceClientListByResourceGroupResponse) (AgriServiceClientListByResourceGroupResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "AgriServiceClient.NewListByResourceGroupPager")
			nextLink := ""
			if page != nil {
				nextLink = *page.NextLink
			}
			resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
				return client.listByResourceGroupCreateRequest(ctx, resourceGroupName, options)
			}, nil)
			if err != nil {
				return AgriServiceClientListByResourceGroupResponse{}, err
			}
			return client.listByResourceGroupHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByResourceGroupCreateRequest creates the ListByResourceGroup request.
func (client *AgriServiceClient) listByResourceGroupCreateRequest(ctx context.Context, resourceGroupName string, _ *AgriServiceClientListByResourceGroupOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AgriculturePlatform/agriServices"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20240601Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByResourceGroupHandleResponse handles the ListByResourceGroup response.
func (client *AgriServiceClient) listByResourceGroupHandleResponse(resp *http.Response) (AgriServiceClientListByResourceGroupResponse, error) {
	result := AgriServiceClientListByResourceGroupResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.AgriServiceResourceListResult); err != nil {
		return AgriServiceClientListByResourceGroupResponse{}, err
	}
	return result, nil
}

// NewListBySubscriptionPager - List AgriServiceResource resources by subscription ID
//   - options - AgriServiceClientListBySubscriptionOptions contains the optional parameters for the AgriServiceClient.NewListBySubscriptionPager
//     method.
func (client *AgriServiceClient) NewListBySubscriptionPager(options *AgriServiceClientListBySubscriptionOptions) *runtime.Pager[AgriServiceClientListBySubscriptionResponse] {
	return runtime.NewPager(runtime.PagingHandler[AgriServiceClientListBySubscriptionResponse]{
		More: func(page AgriServiceClientListBySubscriptionResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *AgriServiceClientListBySubscriptionResponse) (AgriServiceClientListBySubscriptionResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "AgriServiceClient.NewListBySubscriptionPager")
			nextLink := ""
			if page != nil {
				nextLink = *page.NextLink
			}
			resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
				return client.listBySubscriptionCreateRequest(ctx, options)
			}, nil)
			if err != nil {
				return AgriServiceClientListBySubscriptionResponse{}, err
			}
			return client.listBySubscriptionHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listBySubscriptionCreateRequest creates the ListBySubscription request.
func (client *AgriServiceClient) listBySubscriptionCreateRequest(ctx context.Context, _ *AgriServiceClientListBySubscriptionOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.AgriculturePlatform/agriServices"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20240601Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listBySubscriptionHandleResponse handles the ListBySubscription response.
func (client *AgriServiceClient) listBySubscriptionHandleResponse(resp *http.Response) (AgriServiceClientListBySubscriptionResponse, error) {
	result := AgriServiceClientListBySubscriptionResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.AgriServiceResourceListResult); err != nil {
		return AgriServiceClientListBySubscriptionResponse{}, err
	}
	return result, nil
}

// BeginUpdate - Update a AgriServiceResource
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - agriServiceResourceName - The name of the AgriService resource.
//   - properties - The resource properties to be updated.
//   - options - AgriServiceClientBeginUpdateOptions contains the optional parameters for the AgriServiceClient.BeginUpdate method.
func (client *AgriServiceClient) BeginUpdate(ctx context.Context, resourceGroupName string, agriServiceResourceName string, properties AgriServiceResourceUpdate, options *AgriServiceClientBeginUpdateOptions) (*runtime.Poller[AgriServiceClientUpdateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.update(ctx, resourceGroupName, agriServiceResourceName, properties, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[AgriServiceClientUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[AgriServiceClientUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Update - Update a AgriServiceResource
// If the operation fails it returns an *azcore.ResponseError type.
func (client *AgriServiceClient) update(ctx context.Context, resourceGroupName string, agriServiceResourceName string, properties AgriServiceResourceUpdate, options *AgriServiceClientBeginUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "AgriServiceClient.BeginUpdate"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.updateCreateRequest(ctx, resourceGroupName, agriServiceResourceName, properties, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusAccepted) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// updateCreateRequest creates the Update request.
func (client *AgriServiceClient) updateCreateRequest(ctx context.Context, resourceGroupName string, agriServiceResourceName string, properties AgriServiceResourceUpdate, _ *AgriServiceClientBeginUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AgriculturePlatform/agriServices/{agriServiceResourceName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if agriServiceResourceName == "" {
		return nil, errors.New("parameter agriServiceResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{agriServiceResourceName}", url.PathEscape(agriServiceResourceName))
	req, err := runtime.NewRequest(ctx, http.MethodPatch, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20240601Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	req.Raw().Header["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, properties); err != nil {
		return nil, err
	}
	return req, nil
}
