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

// ScheduledActionExtensionClient contains the methods for the ScheduledActionExtension group.
// Don't use this type directly, use NewScheduledActionExtensionClient() instead.
type ScheduledActionExtensionClient struct {
	internal *arm.Client
}

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

// NewListByVMsPager - List ScheduledActionResources resources by parent
//
// Generated from API version 2025-04-15-preview
//   - resourceURI - The fully qualified Azure Resource manager identifier of the resource.
//   - options - ScheduledActionExtensionClientListByVMsOptions contains the optional parameters for the ScheduledActionExtensionClient.NewListByVMsPager
//     method.
func (client *ScheduledActionExtensionClient) NewListByVMsPager(resourceURI string, options *ScheduledActionExtensionClientListByVMsOptions) *runtime.Pager[ScheduledActionExtensionClientListByVMsResponse] {
	return runtime.NewPager(runtime.PagingHandler[ScheduledActionExtensionClientListByVMsResponse]{
		More: func(page ScheduledActionExtensionClientListByVMsResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *ScheduledActionExtensionClientListByVMsResponse) (ScheduledActionExtensionClientListByVMsResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "ScheduledActionExtensionClient.NewListByVMsPager")
			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.listByVMsCreateRequest(ctx, resourceURI, options)
			}, nil)
			if err != nil {
				return ScheduledActionExtensionClientListByVMsResponse{}, err
			}
			return client.listByVMsHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByVMsCreateRequest creates the ListByVMs request.
func (client *ScheduledActionExtensionClient) listByVMsCreateRequest(ctx context.Context, resourceURI string, _ *ScheduledActionExtensionClientListByVMsOptions) (*policy.Request, error) {
	urlPath := "/{resourceUri}/providers/Microsoft.ComputeSchedule/associatedScheduledActions"
	urlPath = strings.ReplaceAll(urlPath, "{resourceUri}", resourceURI)
	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
}

// listByVMsHandleResponse handles the ListByVMs response.
func (client *ScheduledActionExtensionClient) listByVMsHandleResponse(resp *http.Response) (ScheduledActionExtensionClientListByVMsResponse, error) {
	result := ScheduledActionExtensionClientListByVMsResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ScheduledActionResourcesListResult); err != nil {
		return ScheduledActionExtensionClientListByVMsResponse{}, err
	}
	return result, nil
}
