// 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) AutoRest Code Generator. DO NOT EDIT.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

package armstoragecache

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

// AutoExportJobsClient contains the methods for the AutoExportJobs group.
// Don't use this type directly, use NewAutoExportJobsClient() instead.
type AutoExportJobsClient struct {
	internal       *arm.Client
	subscriptionID string
}

// NewAutoExportJobsClient creates a new instance of AutoExportJobsClient with the specified values.
//   - subscriptionID - The ID of the target subscription.
//   - credential - used to authorize requests. Usually a credential from azidentity.
//   - options - pass nil to accept the default values.
func NewAutoExportJobsClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*AutoExportJobsClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &AutoExportJobsClient{
		subscriptionID: subscriptionID,
		internal:       cl,
	}
	return client, nil
}

// BeginCreateOrUpdate - Create or update an auto export job.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-07-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - amlFilesystemName - Name for the AML file system. Allows alphanumerics, underscores, and hyphens. Start and end with alphanumeric.
//   - autoExportJobName - Name for the auto export job. Allows alphanumerics, underscores, and hyphens. Start and end with alphanumeric.
//   - autoExportJob - Object containing the user-selectable properties of the auto export job. If read-only properties are included,
//     they must match the existing values of those properties.
//   - options - AutoExportJobsClientBeginCreateOrUpdateOptions contains the optional parameters for the AutoExportJobsClient.BeginCreateOrUpdate
//     method.
func (client *AutoExportJobsClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, amlFilesystemName string, autoExportJobName string, autoExportJob AutoExportJob, options *AutoExportJobsClientBeginCreateOrUpdateOptions) (*runtime.Poller[AutoExportJobsClientCreateOrUpdateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.createOrUpdate(ctx, resourceGroupName, amlFilesystemName, autoExportJobName, autoExportJob, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[AutoExportJobsClientCreateOrUpdateResponse]{
			FinalStateVia: runtime.FinalStateViaAzureAsyncOp,
			Tracer:        client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[AutoExportJobsClientCreateOrUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// CreateOrUpdate - Create or update an auto export job.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-07-01
func (client *AutoExportJobsClient) createOrUpdate(ctx context.Context, resourceGroupName string, amlFilesystemName string, autoExportJobName string, autoExportJob AutoExportJob, options *AutoExportJobsClientBeginCreateOrUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "AutoExportJobsClient.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, amlFilesystemName, autoExportJobName, autoExportJob, 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 *AutoExportJobsClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, amlFilesystemName string, autoExportJobName string, autoExportJob AutoExportJob, _ *AutoExportJobsClientBeginCreateOrUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.StorageCache/amlFilesystems/{amlFilesystemName}/autoExportJobs/{autoExportJobName}"
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if amlFilesystemName == "" {
		return nil, errors.New("parameter amlFilesystemName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{amlFilesystemName}", url.PathEscape(amlFilesystemName))
	if autoExportJobName == "" {
		return nil, errors.New("parameter autoExportJobName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{autoExportJobName}", url.PathEscape(autoExportJobName))
	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", "2025-07-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, autoExportJob); err != nil {
		return nil, err
	}
	return req, nil
}

// BeginDelete - Schedules an auto export job for deletion.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-07-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - amlFilesystemName - Name for the AML file system. Allows alphanumerics, underscores, and hyphens. Start and end with alphanumeric.
//   - autoExportJobName - Name for the auto export job. Allows alphanumerics, underscores, and hyphens. Start and end with alphanumeric.
//   - options - AutoExportJobsClientBeginDeleteOptions contains the optional parameters for the AutoExportJobsClient.BeginDelete
//     method.
func (client *AutoExportJobsClient) BeginDelete(ctx context.Context, resourceGroupName string, amlFilesystemName string, autoExportJobName string, options *AutoExportJobsClientBeginDeleteOptions) (*runtime.Poller[AutoExportJobsClientDeleteResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.deleteOperation(ctx, resourceGroupName, amlFilesystemName, autoExportJobName, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[AutoExportJobsClientDeleteResponse]{
			FinalStateVia: runtime.FinalStateViaLocation,
			Tracer:        client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[AutoExportJobsClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Delete - Schedules an auto export job for deletion.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-07-01
func (client *AutoExportJobsClient) deleteOperation(ctx context.Context, resourceGroupName string, amlFilesystemName string, autoExportJobName string, options *AutoExportJobsClientBeginDeleteOptions) (*http.Response, error) {
	var err error
	const operationName = "AutoExportJobsClient.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, amlFilesystemName, autoExportJobName, 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 *AutoExportJobsClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, amlFilesystemName string, autoExportJobName string, _ *AutoExportJobsClientBeginDeleteOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.StorageCache/amlFilesystems/{amlFilesystemName}/autoExportJobs/{autoExportJobName}"
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if amlFilesystemName == "" {
		return nil, errors.New("parameter amlFilesystemName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{amlFilesystemName}", url.PathEscape(amlFilesystemName))
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if autoExportJobName == "" {
		return nil, errors.New("parameter autoExportJobName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{autoExportJobName}", url.PathEscape(autoExportJobName))
	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", "2025-07-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// Get - Returns an auto export job.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-07-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - amlFilesystemName - Name for the AML file system. Allows alphanumerics, underscores, and hyphens. Start and end with alphanumeric.
//   - autoExportJobName - Name for the auto export job. Allows alphanumerics, underscores, and hyphens. Start and end with alphanumeric.
//   - options - AutoExportJobsClientGetOptions contains the optional parameters for the AutoExportJobsClient.Get method.
func (client *AutoExportJobsClient) Get(ctx context.Context, resourceGroupName string, amlFilesystemName string, autoExportJobName string, options *AutoExportJobsClientGetOptions) (AutoExportJobsClientGetResponse, error) {
	var err error
	const operationName = "AutoExportJobsClient.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, amlFilesystemName, autoExportJobName, options)
	if err != nil {
		return AutoExportJobsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return AutoExportJobsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return AutoExportJobsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *AutoExportJobsClient) getCreateRequest(ctx context.Context, resourceGroupName string, amlFilesystemName string, autoExportJobName string, _ *AutoExportJobsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.StorageCache/amlFilesystems/{amlFilesystemName}/autoExportJobs/{autoExportJobName}"
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if amlFilesystemName == "" {
		return nil, errors.New("parameter amlFilesystemName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{amlFilesystemName}", url.PathEscape(amlFilesystemName))
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if autoExportJobName == "" {
		return nil, errors.New("parameter autoExportJobName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{autoExportJobName}", url.PathEscape(autoExportJobName))
	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", "2025-07-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// NewListByAmlFilesystemPager - Returns all the auto export jobs the user has access to under an AML File System.
//
// Generated from API version 2025-07-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - amlFilesystemName - Name for the AML file system. Allows alphanumerics, underscores, and hyphens. Start and end with alphanumeric.
//   - options - AutoExportJobsClientListByAmlFilesystemOptions contains the optional parameters for the AutoExportJobsClient.NewListByAmlFilesystemPager
//     method.
func (client *AutoExportJobsClient) NewListByAmlFilesystemPager(resourceGroupName string, amlFilesystemName string, options *AutoExportJobsClientListByAmlFilesystemOptions) *runtime.Pager[AutoExportJobsClientListByAmlFilesystemResponse] {
	return runtime.NewPager(runtime.PagingHandler[AutoExportJobsClientListByAmlFilesystemResponse]{
		More: func(page AutoExportJobsClientListByAmlFilesystemResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *AutoExportJobsClientListByAmlFilesystemResponse) (AutoExportJobsClientListByAmlFilesystemResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "AutoExportJobsClient.NewListByAmlFilesystemPager")
			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.listByAmlFilesystemCreateRequest(ctx, resourceGroupName, amlFilesystemName, options)
			}, nil)
			if err != nil {
				return AutoExportJobsClientListByAmlFilesystemResponse{}, err
			}
			return client.listByAmlFilesystemHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByAmlFilesystemCreateRequest creates the ListByAmlFilesystem request.
func (client *AutoExportJobsClient) listByAmlFilesystemCreateRequest(ctx context.Context, resourceGroupName string, amlFilesystemName string, _ *AutoExportJobsClientListByAmlFilesystemOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.StorageCache/amlFilesystems/{amlFilesystemName}/autoExportJobs"
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if amlFilesystemName == "" {
		return nil, errors.New("parameter amlFilesystemName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{amlFilesystemName}", url.PathEscape(amlFilesystemName))
	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", "2025-07-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByAmlFilesystemHandleResponse handles the ListByAmlFilesystem response.
func (client *AutoExportJobsClient) listByAmlFilesystemHandleResponse(resp *http.Response) (AutoExportJobsClientListByAmlFilesystemResponse, error) {
	result := AutoExportJobsClientListByAmlFilesystemResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.AutoExportJobsListResult); err != nil {
		return AutoExportJobsClientListByAmlFilesystemResponse{}, err
	}
	return result, nil
}

// BeginUpdate - Update an auto export job instance.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-07-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - amlFilesystemName - Name for the AML file system. Allows alphanumerics, underscores, and hyphens. Start and end with alphanumeric.
//   - autoExportJobName - Name for the auto export job. Allows alphanumerics, underscores, and hyphens. Start and end with alphanumeric.
//   - autoExportJob - Object containing the user-selectable properties of the auto export job. If read-only properties are included,
//     they must match the existing values of those properties.
//   - options - AutoExportJobsClientBeginUpdateOptions contains the optional parameters for the AutoExportJobsClient.BeginUpdate
//     method.
func (client *AutoExportJobsClient) BeginUpdate(ctx context.Context, resourceGroupName string, amlFilesystemName string, autoExportJobName string, autoExportJob AutoExportJobUpdate, options *AutoExportJobsClientBeginUpdateOptions) (*runtime.Poller[AutoExportJobsClientUpdateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.update(ctx, resourceGroupName, amlFilesystemName, autoExportJobName, autoExportJob, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[AutoExportJobsClientUpdateResponse]{
			FinalStateVia: runtime.FinalStateViaAzureAsyncOp,
			Tracer:        client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[AutoExportJobsClientUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Update - Update an auto export job instance.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-07-01
func (client *AutoExportJobsClient) update(ctx context.Context, resourceGroupName string, amlFilesystemName string, autoExportJobName string, autoExportJob AutoExportJobUpdate, options *AutoExportJobsClientBeginUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "AutoExportJobsClient.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, amlFilesystemName, autoExportJobName, autoExportJob, 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 *AutoExportJobsClient) updateCreateRequest(ctx context.Context, resourceGroupName string, amlFilesystemName string, autoExportJobName string, autoExportJob AutoExportJobUpdate, _ *AutoExportJobsClientBeginUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.StorageCache/amlFilesystems/{amlFilesystemName}/autoExportJobs/{autoExportJobName}"
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if amlFilesystemName == "" {
		return nil, errors.New("parameter amlFilesystemName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{amlFilesystemName}", url.PathEscape(amlFilesystemName))
	if autoExportJobName == "" {
		return nil, errors.New("parameter autoExportJobName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{autoExportJobName}", url.PathEscape(autoExportJobName))
	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", "2025-07-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, autoExportJob); err != nil {
		return nil, err
	}
	return req, nil
}
