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

// OccurrenceExtensionClient contains the methods for the OccurrenceExtension group.
// Don't use this type directly, use NewOccurrenceExtensionClient() instead.
//
// Generated from API version 2026-04-15-preview
type OccurrenceExtensionClient struct {
	internal *arm.Client
}

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

// NewListOccurrenceByVMsPager - List OccurrenceExtensionResource resources by parent
//   - resourceURI - The fully qualified Azure Resource manager identifier of the resource.
//   - options - OccurrenceExtensionClientListOccurrenceByVMsOptions contains the optional parameters for the OccurrenceExtensionClient.NewListOccurrenceByVMsPager
//     method.
func (client *OccurrenceExtensionClient) NewListOccurrenceByVMsPager(resourceURI string, options *OccurrenceExtensionClientListOccurrenceByVMsOptions) *runtime.Pager[OccurrenceExtensionClientListOccurrenceByVMsResponse] {
	return runtime.NewPager(runtime.PagingHandler[OccurrenceExtensionClientListOccurrenceByVMsResponse]{
		More: func(page OccurrenceExtensionClientListOccurrenceByVMsResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *OccurrenceExtensionClientListOccurrenceByVMsResponse) (OccurrenceExtensionClientListOccurrenceByVMsResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "OccurrenceExtensionClient.NewListOccurrenceByVMsPager")
			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.listOccurrenceByVMsCreateRequest(ctx, resourceURI, options)
			}, nil)
			if err != nil {
				return OccurrenceExtensionClientListOccurrenceByVMsResponse{}, err
			}
			return client.listOccurrenceByVMsHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listOccurrenceByVMsCreateRequest creates the ListOccurrenceByVMs request.
func (client *OccurrenceExtensionClient) listOccurrenceByVMsCreateRequest(ctx context.Context, resourceURI string, _ *OccurrenceExtensionClientListOccurrenceByVMsOptions) (*policy.Request, error) {
	urlPath := "/{resourceUri}/providers/Microsoft.ComputeSchedule/associatedOccurrences"
	if resourceURI == "" {
		return nil, errors.New("parameter resourceURI cannot be empty")
	}
	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", version20260415Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listOccurrenceByVMsHandleResponse handles the ListOccurrenceByVMs response.
func (client *OccurrenceExtensionClient) listOccurrenceByVMsHandleResponse(resp *http.Response) (OccurrenceExtensionClientListOccurrenceByVMsResponse, error) {
	result := OccurrenceExtensionClientListOccurrenceByVMsResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.OccurrenceExtensionResourceListResult); err != nil {
		return OccurrenceExtensionClientListOccurrenceByVMsResponse{}, err
	}
	return result, nil
}
