// 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 armcommvaultcontentstore

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"
)

// CloudAccountsClient contains the methods for the CloudAccounts group.
// Don't use this type directly, use NewCloudAccountsClient() instead.
//
// Generated from API version 2026-07-03-preview
type CloudAccountsClient struct {
	internal       *arm.Client
	subscriptionID string
}

// NewCloudAccountsClient creates a new instance of CloudAccountsClient 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 NewCloudAccountsClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*CloudAccountsClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &CloudAccountsClient{
		subscriptionID: subscriptionID,
		internal:       cl,
	}
	return client, nil
}

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

// CreateOrUpdate - Create a CloudAccount
// If the operation fails it returns an *azcore.ResponseError type.
func (client *CloudAccountsClient) createOrUpdate(ctx context.Context, resourceGroupName string, cloudAccountName string, resource CloudAccount, options *CloudAccountsClientBeginCreateOrUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "CloudAccountsClient.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, cloudAccountName, 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 *CloudAccountsClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, cloudAccountName string, resource CloudAccount, _ *CloudAccountsClientBeginCreateOrUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Commvault.ContentStore/cloudAccounts/{cloudAccountName}"
	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 cloudAccountName == "" {
		return nil, errors.New("parameter cloudAccountName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{cloudAccountName}", url.PathEscape(cloudAccountName))
	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", version20260703Preview)
	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 CloudAccount
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - cloudAccountName - Name of the Cloud Account resource
//   - options - CloudAccountsClientBeginDeleteOptions contains the optional parameters for the CloudAccountsClient.BeginDelete
//     method.
func (client *CloudAccountsClient) BeginDelete(ctx context.Context, resourceGroupName string, cloudAccountName string, options *CloudAccountsClientBeginDeleteOptions) (*runtime.Poller[CloudAccountsClientDeleteResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.deleteOperation(ctx, resourceGroupName, cloudAccountName, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[CloudAccountsClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[CloudAccountsClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Delete - Delete a CloudAccount
// If the operation fails it returns an *azcore.ResponseError type.
func (client *CloudAccountsClient) deleteOperation(ctx context.Context, resourceGroupName string, cloudAccountName string, options *CloudAccountsClientBeginDeleteOptions) (*http.Response, error) {
	var err error
	const operationName = "CloudAccountsClient.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, cloudAccountName, 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 *CloudAccountsClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, cloudAccountName string, _ *CloudAccountsClientBeginDeleteOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Commvault.ContentStore/cloudAccounts/{cloudAccountName}"
	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 cloudAccountName == "" {
		return nil, errors.New("parameter cloudAccountName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{cloudAccountName}", url.PathEscape(cloudAccountName))
	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", version20260703Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	return req, nil
}

// Get - Get a CloudAccount
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - cloudAccountName - Name of the Cloud Account resource
//   - options - CloudAccountsClientGetOptions contains the optional parameters for the CloudAccountsClient.Get method.
func (client *CloudAccountsClient) Get(ctx context.Context, resourceGroupName string, cloudAccountName string, options *CloudAccountsClientGetOptions) (CloudAccountsClientGetResponse, error) {
	var err error
	const operationName = "CloudAccountsClient.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, cloudAccountName, options)
	if err != nil {
		return CloudAccountsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return CloudAccountsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return CloudAccountsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *CloudAccountsClient) getCreateRequest(ctx context.Context, resourceGroupName string, cloudAccountName string, _ *CloudAccountsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Commvault.ContentStore/cloudAccounts/{cloudAccountName}"
	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 cloudAccountName == "" {
		return nil, errors.New("parameter cloudAccountName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{cloudAccountName}", url.PathEscape(cloudAccountName))
	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", version20260703Preview)
	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 *CloudAccountsClient) getHandleResponse(resp *http.Response) (CloudAccountsClientGetResponse, error) {
	result := CloudAccountsClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.CloudAccount); err != nil {
		return CloudAccountsClientGetResponse{}, err
	}
	return result, nil
}

// LatestLinkedSaaS - Returns the latest SaaS linked to the cloud account.
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - cloudAccountName - Name of the Cloud Account resource
//   - options - CloudAccountsClientLatestLinkedSaaSOptions contains the optional parameters for the CloudAccountsClient.LatestLinkedSaaS
//     method.
func (client *CloudAccountsClient) LatestLinkedSaaS(ctx context.Context, resourceGroupName string, cloudAccountName string, options *CloudAccountsClientLatestLinkedSaaSOptions) (CloudAccountsClientLatestLinkedSaaSResponse, error) {
	var err error
	const operationName = "CloudAccountsClient.LatestLinkedSaaS"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.latestLinkedSaaSCreateRequest(ctx, resourceGroupName, cloudAccountName, options)
	if err != nil {
		return CloudAccountsClientLatestLinkedSaaSResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return CloudAccountsClientLatestLinkedSaaSResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return CloudAccountsClientLatestLinkedSaaSResponse{}, err
	}
	resp, err := client.latestLinkedSaaSHandleResponse(httpResp)
	return resp, err
}

// latestLinkedSaaSCreateRequest creates the LatestLinkedSaaS request.
func (client *CloudAccountsClient) latestLinkedSaaSCreateRequest(ctx context.Context, resourceGroupName string, cloudAccountName string, _ *CloudAccountsClientLatestLinkedSaaSOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Commvault.ContentStore/cloudAccounts/{cloudAccountName}/latestLinkedSaaS"
	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 cloudAccountName == "" {
		return nil, errors.New("parameter cloudAccountName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{cloudAccountName}", url.PathEscape(cloudAccountName))
	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", version20260703Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// latestLinkedSaaSHandleResponse handles the LatestLinkedSaaS response.
func (client *CloudAccountsClient) latestLinkedSaaSHandleResponse(resp *http.Response) (CloudAccountsClientLatestLinkedSaaSResponse, error) {
	result := CloudAccountsClientLatestLinkedSaaSResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.LatestLinkedSaaSResponse); err != nil {
		return CloudAccountsClientLatestLinkedSaaSResponse{}, err
	}
	return result, nil
}

// BeginLinkSaaS - Links a new SaaS to the cloud account.
//
// Links a new SaaS to the cloud account.
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - cloudAccountName - Name of the Cloud Account resource
//   - body - The content of the action request
//   - options - CloudAccountsClientBeginLinkSaaSOptions contains the optional parameters for the CloudAccountsClient.BeginLinkSaaS
//     method.
func (client *CloudAccountsClient) BeginLinkSaaS(ctx context.Context, resourceGroupName string, cloudAccountName string, body SaaSData, options *CloudAccountsClientBeginLinkSaaSOptions) (*runtime.Poller[CloudAccountsClientLinkSaaSResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.linkSaaS(ctx, resourceGroupName, cloudAccountName, body, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[CloudAccountsClientLinkSaaSResponse]{
			FinalStateVia: runtime.FinalStateViaAzureAsyncOp,
			Tracer:        client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[CloudAccountsClientLinkSaaSResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// LinkSaaS - Links a new SaaS to the cloud account.
//
// Links a new SaaS to the cloud account.
// If the operation fails it returns an *azcore.ResponseError type.
func (client *CloudAccountsClient) linkSaaS(ctx context.Context, resourceGroupName string, cloudAccountName string, body SaaSData, options *CloudAccountsClientBeginLinkSaaSOptions) (*http.Response, error) {
	var err error
	const operationName = "CloudAccountsClient.BeginLinkSaaS"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.linkSaaSCreateRequest(ctx, resourceGroupName, cloudAccountName, body, 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
}

// linkSaaSCreateRequest creates the LinkSaaS request.
func (client *CloudAccountsClient) linkSaaSCreateRequest(ctx context.Context, resourceGroupName string, cloudAccountName string, body SaaSData, _ *CloudAccountsClientBeginLinkSaaSOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Commvault.ContentStore/cloudAccounts/{cloudAccountName}/linkSaaS"
	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 cloudAccountName == "" {
		return nil, errors.New("parameter cloudAccountName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{cloudAccountName}", url.PathEscape(cloudAccountName))
	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", version20260703Preview)
	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, body); err != nil {
		return nil, err
	}
	return req, nil
}

// NewListByResourceGroupPager - List CloudAccount resources by resource group
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - options - CloudAccountsClientListByResourceGroupOptions contains the optional parameters for the CloudAccountsClient.NewListByResourceGroupPager
//     method.
func (client *CloudAccountsClient) NewListByResourceGroupPager(resourceGroupName string, options *CloudAccountsClientListByResourceGroupOptions) *runtime.Pager[CloudAccountsClientListByResourceGroupResponse] {
	return runtime.NewPager(runtime.PagingHandler[CloudAccountsClientListByResourceGroupResponse]{
		More: func(page CloudAccountsClientListByResourceGroupResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *CloudAccountsClientListByResourceGroupResponse) (CloudAccountsClientListByResourceGroupResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "CloudAccountsClient.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 CloudAccountsClientListByResourceGroupResponse{}, err
			}
			return client.listByResourceGroupHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByResourceGroupCreateRequest creates the ListByResourceGroup request.
func (client *CloudAccountsClient) listByResourceGroupCreateRequest(ctx context.Context, resourceGroupName string, _ *CloudAccountsClientListByResourceGroupOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Commvault.ContentStore/cloudAccounts"
	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", version20260703Preview)
	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 *CloudAccountsClient) listByResourceGroupHandleResponse(resp *http.Response) (CloudAccountsClientListByResourceGroupResponse, error) {
	result := CloudAccountsClientListByResourceGroupResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.CloudAccountListResult); err != nil {
		return CloudAccountsClientListByResourceGroupResponse{}, err
	}
	return result, nil
}

// NewListBySubscriptionPager - List CloudAccount resources by subscription ID
//   - options - CloudAccountsClientListBySubscriptionOptions contains the optional parameters for the CloudAccountsClient.NewListBySubscriptionPager
//     method.
func (client *CloudAccountsClient) NewListBySubscriptionPager(options *CloudAccountsClientListBySubscriptionOptions) *runtime.Pager[CloudAccountsClientListBySubscriptionResponse] {
	return runtime.NewPager(runtime.PagingHandler[CloudAccountsClientListBySubscriptionResponse]{
		More: func(page CloudAccountsClientListBySubscriptionResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *CloudAccountsClientListBySubscriptionResponse) (CloudAccountsClientListBySubscriptionResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "CloudAccountsClient.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 CloudAccountsClientListBySubscriptionResponse{}, err
			}
			return client.listBySubscriptionHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listBySubscriptionCreateRequest creates the ListBySubscription request.
func (client *CloudAccountsClient) listBySubscriptionCreateRequest(ctx context.Context, _ *CloudAccountsClientListBySubscriptionOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Commvault.ContentStore/cloudAccounts"
	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", version20260703Preview)
	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 *CloudAccountsClient) listBySubscriptionHandleResponse(resp *http.Response) (CloudAccountsClientListBySubscriptionResponse, error) {
	result := CloudAccountsClientListBySubscriptionResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.CloudAccountListResult); err != nil {
		return CloudAccountsClientListBySubscriptionResponse{}, err
	}
	return result, nil
}

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

// Update - Update a CloudAccount
// If the operation fails it returns an *azcore.ResponseError type.
func (client *CloudAccountsClient) update(ctx context.Context, resourceGroupName string, cloudAccountName string, properties CloudAccountUpdate, options *CloudAccountsClientBeginUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "CloudAccountsClient.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, cloudAccountName, 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 *CloudAccountsClient) updateCreateRequest(ctx context.Context, resourceGroupName string, cloudAccountName string, properties CloudAccountUpdate, _ *CloudAccountsClientBeginUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Commvault.ContentStore/cloudAccounts/{cloudAccountName}"
	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 cloudAccountName == "" {
		return nil, errors.New("parameter cloudAccountName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{cloudAccountName}", url.PathEscape(cloudAccountName))
	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", version20260703Preview)
	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
}
