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

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

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

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

// Cancel - A synchronous resource action.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-04-15-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - scheduledActionName - The name of the ScheduledAction
//   - occurrenceID - The name of the Occurrence
//   - body - The content of the action request
//   - options - OccurrencesClientCancelOptions contains the optional parameters for the OccurrencesClient.Cancel method.
func (client *OccurrencesClient) Cancel(ctx context.Context, resourceGroupName string, scheduledActionName string, occurrenceID string, body CancelOccurrenceRequest, options *OccurrencesClientCancelOptions) (OccurrencesClientCancelResponse, error) {
	var err error
	const operationName = "OccurrencesClient.Cancel"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.cancelCreateRequest(ctx, resourceGroupName, scheduledActionName, occurrenceID, body, options)
	if err != nil {
		return OccurrencesClientCancelResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return OccurrencesClientCancelResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return OccurrencesClientCancelResponse{}, err
	}
	resp, err := client.cancelHandleResponse(httpResp)
	return resp, err
}

// cancelCreateRequest creates the Cancel request.
func (client *OccurrencesClient) cancelCreateRequest(ctx context.Context, resourceGroupName string, scheduledActionName string, occurrenceID string, body CancelOccurrenceRequest, _ *OccurrencesClientCancelOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ComputeSchedule/scheduledActions/{scheduledActionName}/occurrences/{occurrenceId}/cancel"
	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 scheduledActionName == "" {
		return nil, errors.New("parameter scheduledActionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{scheduledActionName}", url.PathEscape(scheduledActionName))
	if occurrenceID == "" {
		return nil, errors.New("parameter occurrenceID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{occurrenceId}", url.PathEscape(occurrenceID))
	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", "2025-04-15-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	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
}

// cancelHandleResponse handles the Cancel response.
func (client *OccurrencesClient) cancelHandleResponse(resp *http.Response) (OccurrencesClientCancelResponse, error) {
	result := OccurrencesClientCancelResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.RecurringActionsResourceOperationResult); err != nil {
		return OccurrencesClientCancelResponse{}, err
	}
	return result, nil
}

// BeginDelay - A long-running resource action.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-04-15-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - scheduledActionName - The name of the ScheduledAction
//   - occurrenceID - The name of the Occurrence
//   - body - The content of the action request
//   - options - OccurrencesClientBeginDelayOptions contains the optional parameters for the OccurrencesClient.BeginDelay method.
func (client *OccurrencesClient) BeginDelay(ctx context.Context, resourceGroupName string, scheduledActionName string, occurrenceID string, body DelayRequest, options *OccurrencesClientBeginDelayOptions) (*runtime.Poller[OccurrencesClientDelayResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.delay(ctx, resourceGroupName, scheduledActionName, occurrenceID, body, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[OccurrencesClientDelayResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[OccurrencesClientDelayResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Delay - A long-running resource action.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-04-15-preview
func (client *OccurrencesClient) delay(ctx context.Context, resourceGroupName string, scheduledActionName string, occurrenceID string, body DelayRequest, options *OccurrencesClientBeginDelayOptions) (*http.Response, error) {
	var err error
	const operationName = "OccurrencesClient.BeginDelay"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.delayCreateRequest(ctx, resourceGroupName, scheduledActionName, occurrenceID, 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
}

// delayCreateRequest creates the Delay request.
func (client *OccurrencesClient) delayCreateRequest(ctx context.Context, resourceGroupName string, scheduledActionName string, occurrenceID string, body DelayRequest, _ *OccurrencesClientBeginDelayOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ComputeSchedule/scheduledActions/{scheduledActionName}/occurrences/{occurrenceId}/delay"
	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 scheduledActionName == "" {
		return nil, errors.New("parameter scheduledActionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{scheduledActionName}", url.PathEscape(scheduledActionName))
	if occurrenceID == "" {
		return nil, errors.New("parameter occurrenceID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{occurrenceId}", url.PathEscape(occurrenceID))
	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", "2025-04-15-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	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
}

// Get - Get a Occurrence
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-04-15-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - scheduledActionName - The name of the ScheduledAction
//   - occurrenceID - The name of the Occurrence
//   - options - OccurrencesClientGetOptions contains the optional parameters for the OccurrencesClient.Get method.
func (client *OccurrencesClient) Get(ctx context.Context, resourceGroupName string, scheduledActionName string, occurrenceID string, options *OccurrencesClientGetOptions) (OccurrencesClientGetResponse, error) {
	var err error
	const operationName = "OccurrencesClient.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, scheduledActionName, occurrenceID, options)
	if err != nil {
		return OccurrencesClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return OccurrencesClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return OccurrencesClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *OccurrencesClient) getCreateRequest(ctx context.Context, resourceGroupName string, scheduledActionName string, occurrenceID string, _ *OccurrencesClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ComputeSchedule/scheduledActions/{scheduledActionName}/occurrences/{occurrenceId}"
	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 scheduledActionName == "" {
		return nil, errors.New("parameter scheduledActionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{scheduledActionName}", url.PathEscape(scheduledActionName))
	if occurrenceID == "" {
		return nil, errors.New("parameter occurrenceID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{occurrenceId}", url.PathEscape(occurrenceID))
	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-04-15-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// NewListByScheduledActionPager - List Occurrence resources by ScheduledAction
//
// Generated from API version 2025-04-15-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - scheduledActionName - The name of the ScheduledAction
//   - options - OccurrencesClientListByScheduledActionOptions contains the optional parameters for the OccurrencesClient.NewListByScheduledActionPager
//     method.
func (client *OccurrencesClient) NewListByScheduledActionPager(resourceGroupName string, scheduledActionName string, options *OccurrencesClientListByScheduledActionOptions) *runtime.Pager[OccurrencesClientListByScheduledActionResponse] {
	return runtime.NewPager(runtime.PagingHandler[OccurrencesClientListByScheduledActionResponse]{
		More: func(page OccurrencesClientListByScheduledActionResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *OccurrencesClientListByScheduledActionResponse) (OccurrencesClientListByScheduledActionResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "OccurrencesClient.NewListByScheduledActionPager")
			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.listByScheduledActionCreateRequest(ctx, resourceGroupName, scheduledActionName, options)
			}, nil)
			if err != nil {
				return OccurrencesClientListByScheduledActionResponse{}, err
			}
			return client.listByScheduledActionHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByScheduledActionCreateRequest creates the ListByScheduledAction request.
func (client *OccurrencesClient) listByScheduledActionCreateRequest(ctx context.Context, resourceGroupName string, scheduledActionName string, _ *OccurrencesClientListByScheduledActionOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ComputeSchedule/scheduledActions/{scheduledActionName}/occurrences"
	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 scheduledActionName == "" {
		return nil, errors.New("parameter scheduledActionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{scheduledActionName}", url.PathEscape(scheduledActionName))
	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-04-15-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByScheduledActionHandleResponse handles the ListByScheduledAction response.
func (client *OccurrencesClient) listByScheduledActionHandleResponse(resp *http.Response) (OccurrencesClientListByScheduledActionResponse, error) {
	result := OccurrencesClientListByScheduledActionResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.OccurrenceListResult); err != nil {
		return OccurrencesClientListByScheduledActionResponse{}, err
	}
	return result, nil
}

// NewListResourcesPager - List resources attached to Scheduled Actions for the given occurrence
//
// Generated from API version 2025-04-15-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - scheduledActionName - The name of the ScheduledAction
//   - occurrenceID - The name of the Occurrence
//   - options - OccurrencesClientListResourcesOptions contains the optional parameters for the OccurrencesClient.NewListResourcesPager
//     method.
func (client *OccurrencesClient) NewListResourcesPager(resourceGroupName string, scheduledActionName string, occurrenceID string, options *OccurrencesClientListResourcesOptions) *runtime.Pager[OccurrencesClientListResourcesResponse] {
	return runtime.NewPager(runtime.PagingHandler[OccurrencesClientListResourcesResponse]{
		More: func(page OccurrencesClientListResourcesResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *OccurrencesClientListResourcesResponse) (OccurrencesClientListResourcesResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "OccurrencesClient.NewListResourcesPager")
			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.listResourcesCreateRequest(ctx, resourceGroupName, scheduledActionName, occurrenceID, options)
			}, nil)
			if err != nil {
				return OccurrencesClientListResourcesResponse{}, err
			}
			return client.listResourcesHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listResourcesCreateRequest creates the ListResources request.
func (client *OccurrencesClient) listResourcesCreateRequest(ctx context.Context, resourceGroupName string, scheduledActionName string, occurrenceID string, _ *OccurrencesClientListResourcesOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ComputeSchedule/scheduledActions/{scheduledActionName}/occurrences/{occurrenceId}/resources"
	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 scheduledActionName == "" {
		return nil, errors.New("parameter scheduledActionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{scheduledActionName}", url.PathEscape(scheduledActionName))
	if occurrenceID == "" {
		return nil, errors.New("parameter occurrenceID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{occurrenceId}", url.PathEscape(occurrenceID))
	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-04-15-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listResourcesHandleResponse handles the ListResources response.
func (client *OccurrencesClient) listResourcesHandleResponse(resp *http.Response) (OccurrencesClientListResourcesResponse, error) {
	result := OccurrencesClientListResourcesResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.OccurrenceResourceListResponse); err != nil {
		return OccurrencesClientListResourcesResponse{}, err
	}
	return result, nil
}
