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

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

// VirtualMachineScaleSetLifeCycleHookEventsClient contains the methods for the VirtualMachineScaleSetLifeCycleHookEvents
// group.
// Don't use this type directly, use NewVirtualMachineScaleSetLifeCycleHookEventsClient() instead.
//
// Generated from API version 2026-03-01
type VirtualMachineScaleSetLifeCycleHookEventsClient struct {
	internal       *arm.Client
	subscriptionID string
}

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

// Get - Gets a virtual machine scale set lifecycle hook event.
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - vmScaleSetName - The name of the VM scale set.
//   - lifecycleHookEventName - The name of the VMScaleSetLifecycleHookEvent
//   - options - VirtualMachineScaleSetLifeCycleHookEventsClientGetOptions contains the optional parameters for the VirtualMachineScaleSetLifeCycleHookEventsClient.Get
//     method.
func (client *VirtualMachineScaleSetLifeCycleHookEventsClient) Get(ctx context.Context, resourceGroupName string, vmScaleSetName string, lifecycleHookEventName string, options *VirtualMachineScaleSetLifeCycleHookEventsClientGetOptions) (VirtualMachineScaleSetLifeCycleHookEventsClientGetResponse, error) {
	var err error
	const operationName = "VirtualMachineScaleSetLifeCycleHookEventsClient.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, vmScaleSetName, lifecycleHookEventName, options)
	if err != nil {
		return VirtualMachineScaleSetLifeCycleHookEventsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return VirtualMachineScaleSetLifeCycleHookEventsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return VirtualMachineScaleSetLifeCycleHookEventsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *VirtualMachineScaleSetLifeCycleHookEventsClient) getCreateRequest(ctx context.Context, resourceGroupName string, vmScaleSetName string, lifecycleHookEventName string, _ *VirtualMachineScaleSetLifeCycleHookEventsClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachineScaleSets/{vmScaleSetName}/lifecycleHookEvents/{lifecycleHookEventName}"
	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 vmScaleSetName == "" {
		return nil, errors.New("parameter vmScaleSetName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{vmScaleSetName}", url.PathEscape(vmScaleSetName))
	if lifecycleHookEventName == "" {
		return nil, errors.New("parameter lifecycleHookEventName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{lifecycleHookEventName}", url.PathEscape(lifecycleHookEventName))
	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", version20260301)
	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 *VirtualMachineScaleSetLifeCycleHookEventsClient) getHandleResponse(resp *http.Response) (VirtualMachineScaleSetLifeCycleHookEventsClientGetResponse, error) {
	result := VirtualMachineScaleSetLifeCycleHookEventsClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.VMScaleSetLifecycleHookEvent); err != nil {
		return VirtualMachineScaleSetLifeCycleHookEventsClientGetResponse{}, err
	}
	return result, nil
}

// NewListPager - Gets a list of virtual machine scale set lifecycle hook events created for a virtual machine scale set resource.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - vmScaleSetName - The name of the VM scale set.
//   - options - VirtualMachineScaleSetLifeCycleHookEventsClientListOptions contains the optional parameters for the VirtualMachineScaleSetLifeCycleHookEventsClient.NewListPager
//     method.
func (client *VirtualMachineScaleSetLifeCycleHookEventsClient) NewListPager(resourceGroupName string, vmScaleSetName string, options *VirtualMachineScaleSetLifeCycleHookEventsClientListOptions) *runtime.Pager[VirtualMachineScaleSetLifeCycleHookEventsClientListResponse] {
	return runtime.NewPager(runtime.PagingHandler[VirtualMachineScaleSetLifeCycleHookEventsClientListResponse]{
		More: func(page VirtualMachineScaleSetLifeCycleHookEventsClientListResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *VirtualMachineScaleSetLifeCycleHookEventsClientListResponse) (VirtualMachineScaleSetLifeCycleHookEventsClientListResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "VirtualMachineScaleSetLifeCycleHookEventsClient.NewListPager")
			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.listCreateRequest(ctx, resourceGroupName, vmScaleSetName, options)
			}, nil)
			if err != nil {
				return VirtualMachineScaleSetLifeCycleHookEventsClientListResponse{}, err
			}
			return client.listHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listCreateRequest creates the List request.
func (client *VirtualMachineScaleSetLifeCycleHookEventsClient) listCreateRequest(ctx context.Context, resourceGroupName string, vmScaleSetName string, _ *VirtualMachineScaleSetLifeCycleHookEventsClientListOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachineScaleSets/{vmScaleSetName}/lifecycleHookEvents"
	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 vmScaleSetName == "" {
		return nil, errors.New("parameter vmScaleSetName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{vmScaleSetName}", url.PathEscape(vmScaleSetName))
	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", version20260301)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listHandleResponse handles the List response.
func (client *VirtualMachineScaleSetLifeCycleHookEventsClient) listHandleResponse(resp *http.Response) (VirtualMachineScaleSetLifeCycleHookEventsClientListResponse, error) {
	result := VirtualMachineScaleSetLifeCycleHookEventsClientListResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.VMScaleSetLifecycleHookEventListResult); err != nil {
		return VirtualMachineScaleSetLifeCycleHookEventsClientListResponse{}, err
	}
	return result, nil
}

// Update - The operation to update a virtual machine scale set lifecycle hook event.
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - vmScaleSetName - The name of the VM scale set.
//   - lifecycleHookEventName - The name of the VMScaleSetLifecycleHookEvent
//   - properties - Parameters supplied to the Update virtual machine scale set lifecycle hook event operation.
//   - options - VirtualMachineScaleSetLifeCycleHookEventsClientUpdateOptions contains the optional parameters for the VirtualMachineScaleSetLifeCycleHookEventsClient.Update
//     method.
func (client *VirtualMachineScaleSetLifeCycleHookEventsClient) Update(ctx context.Context, resourceGroupName string, vmScaleSetName string, lifecycleHookEventName string, properties VMScaleSetLifecycleHookEventUpdate, options *VirtualMachineScaleSetLifeCycleHookEventsClientUpdateOptions) (VirtualMachineScaleSetLifeCycleHookEventsClientUpdateResponse, error) {
	var err error
	const operationName = "VirtualMachineScaleSetLifeCycleHookEventsClient.Update"
	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, vmScaleSetName, lifecycleHookEventName, properties, options)
	if err != nil {
		return VirtualMachineScaleSetLifeCycleHookEventsClientUpdateResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return VirtualMachineScaleSetLifeCycleHookEventsClientUpdateResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return VirtualMachineScaleSetLifeCycleHookEventsClientUpdateResponse{}, err
	}
	resp, err := client.updateHandleResponse(httpResp)
	return resp, err
}

// updateCreateRequest creates the Update request.
func (client *VirtualMachineScaleSetLifeCycleHookEventsClient) updateCreateRequest(ctx context.Context, resourceGroupName string, vmScaleSetName string, lifecycleHookEventName string, properties VMScaleSetLifecycleHookEventUpdate, _ *VirtualMachineScaleSetLifeCycleHookEventsClientUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/virtualMachineScaleSets/{vmScaleSetName}/lifecycleHookEvents/{lifecycleHookEventName}"
	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 vmScaleSetName == "" {
		return nil, errors.New("parameter vmScaleSetName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{vmScaleSetName}", url.PathEscape(vmScaleSetName))
	if lifecycleHookEventName == "" {
		return nil, errors.New("parameter lifecycleHookEventName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{lifecycleHookEventName}", url.PathEscape(lifecycleHookEventName))
	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", version20260301)
	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
}

// updateHandleResponse handles the Update response.
func (client *VirtualMachineScaleSetLifeCycleHookEventsClient) updateHandleResponse(resp *http.Response) (VirtualMachineScaleSetLifeCycleHookEventsClientUpdateResponse, error) {
	result := VirtualMachineScaleSetLifeCycleHookEventsClientUpdateResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.VMScaleSetLifecycleHookEvent); err != nil {
		return VirtualMachineScaleSetLifeCycleHookEventsClientUpdateResponse{}, err
	}
	return result, nil
}
