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

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

// ScheduledEventClient contains the methods for the ScheduledEvent group.
// Don't use this type directly, use NewScheduledEventClient() instead.
//
// Generated from API version 2023-10-01-preview
type ScheduledEventClient struct {
	internal       *arm.Client
	subscriptionID string
}

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

// Acknowledge - Post Scheduled Event Acknowledgement
//
// Post Scheduled Event Acknowledgement
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - resourceType - Resource type
//   - resourceName - Resource Name
//   - scheduledEventID - Scheduled Event Id. This is a GUID-formatted string (e.g. 00000000-0000-0000-0000-000000000000)
//   - options - ScheduledEventClientAcknowledgeOptions contains the optional parameters for the ScheduledEventClient.Acknowledge
//     method.
func (client *ScheduledEventClient) Acknowledge(ctx context.Context, resourceGroupName string, resourceType string, resourceName string, scheduledEventID string, options *ScheduledEventClientAcknowledgeOptions) (ScheduledEventClientAcknowledgeResponse, error) {
	var err error
	const operationName = "ScheduledEventClient.Acknowledge"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.acknowledgeCreateRequest(ctx, resourceGroupName, resourceType, resourceName, scheduledEventID, options)
	if err != nil {
		return ScheduledEventClientAcknowledgeResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ScheduledEventClientAcknowledgeResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return ScheduledEventClientAcknowledgeResponse{}, err
	}
	resp, err := client.acknowledgeHandleResponse(httpResp)
	return resp, err
}

// acknowledgeCreateRequest creates the Acknowledge request.
func (client *ScheduledEventClient) acknowledgeCreateRequest(ctx context.Context, resourceGroupName string, resourceType string, resourceName string, scheduledEventID string, _ *ScheduledEventClientAcknowledgeOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.Compute/{resourceType}/{resourceName}/providers/Microsoft.Maintenance/scheduledevents/{scheduledEventId}/acknowledge"
	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 resourceType == "" {
		return nil, errors.New("parameter resourceType cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceType}", url.PathEscape(resourceType))
	if resourceName == "" {
		return nil, errors.New("parameter resourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceName}", url.PathEscape(resourceName))
	if scheduledEventID == "" {
		return nil, errors.New("parameter scheduledEventID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{scheduledEventId}", url.PathEscape(scheduledEventID))
	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", version20231001Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// acknowledgeHandleResponse handles the Acknowledge response.
func (client *ScheduledEventClient) acknowledgeHandleResponse(resp *http.Response) (ScheduledEventClientAcknowledgeResponse, error) {
	result := ScheduledEventClientAcknowledgeResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ScheduledEventApproveResponse); err != nil {
		return ScheduledEventClientAcknowledgeResponse{}, err
	}
	return result, nil
}
