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

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

// PTUQuotaClient contains the methods for the PTUQuota group.
// Don't use this type directly, use NewPTUQuotaClient() instead.
//
// Generated from API version 2026-03-15-preview
type PTUQuotaClient struct {
	internal       *arm.Client
	subscriptionID string
}

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

// GetAvailable - Get available MaaS PTU quota.
//
// Get available MaaS PTU quota.
// If the operation fails it returns an *azcore.ResponseError type.
//   - location - The location name.
//   - options - PTUQuotaClientGetAvailableOptions contains the optional parameters for the PTUQuotaClient.GetAvailable method.
func (client *PTUQuotaClient) GetAvailable(ctx context.Context, location string, options *PTUQuotaClientGetAvailableOptions) (PTUQuotaClientGetAvailableResponse, error) {
	var err error
	const operationName = "PTUQuotaClient.GetAvailable"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.getAvailableCreateRequest(ctx, location, options)
	if err != nil {
		return PTUQuotaClientGetAvailableResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return PTUQuotaClientGetAvailableResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return PTUQuotaClientGetAvailableResponse{}, err
	}
	resp, err := client.getAvailableHandleResponse(httpResp)
	return resp, err
}

// getAvailableCreateRequest creates the GetAvailable request.
func (client *PTUQuotaClient) getAvailableCreateRequest(ctx context.Context, location string, _ *PTUQuotaClientGetAvailableOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.MachineLearningServices/locations/{location}/availableQuota/default"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if location == "" {
		return nil, errors.New("parameter location cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{location}", url.PathEscape(location))
	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", version20260315Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getAvailableHandleResponse handles the GetAvailable response.
func (client *PTUQuotaClient) getAvailableHandleResponse(resp *http.Response) (PTUQuotaClientGetAvailableResponse, error) {
	result := PTUQuotaClientGetAvailableResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.AvailableQuota); err != nil {
		return PTUQuotaClientGetAvailableResponse{}, err
	}
	return result, nil
}

// NewListPager - List MaaS PTU usage and quota.
//
// List MaaS PTU usage and quota.
//   - location - The location name.
//   - options - PTUQuotaClientListOptions contains the optional parameters for the PTUQuotaClient.NewListPager method.
func (client *PTUQuotaClient) NewListPager(location string, options *PTUQuotaClientListOptions) *runtime.Pager[PTUQuotaClientListResponse] {
	return runtime.NewPager(runtime.PagingHandler[PTUQuotaClientListResponse]{
		More: func(page PTUQuotaClientListResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *PTUQuotaClientListResponse) (PTUQuotaClientListResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "PTUQuotaClient.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, location, options)
			}, nil)
			if err != nil {
				return PTUQuotaClientListResponse{}, err
			}
			return client.listHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listCreateRequest creates the List request.
func (client *PTUQuotaClient) listCreateRequest(ctx context.Context, location string, options *PTUQuotaClientListOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.MachineLearningServices/locations/{location}/quotaAndUsage"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if location == "" {
		return nil, errors.New("parameter location cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{location}", url.PathEscape(location))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	if options != nil && options.Skip != nil {
		reqQP.Set("$skip", *options.Skip)
	}
	reqQP.Set("api-version", version20260315Preview)
	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 *PTUQuotaClient) listHandleResponse(resp *http.Response) (PTUQuotaClientListResponse, error) {
	result := PTUQuotaClientListResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.UsageAndQuotaDetailsArmPaginatedResult); err != nil {
		return PTUQuotaClientListResponse{}, err
	}
	return result, nil
}

// NewListAvailablePager - List available MaaS PTU quota.
//
// List available MaaS PTU quota.
//   - location - The location name.
//   - options - PTUQuotaClientListAvailableOptions contains the optional parameters for the PTUQuotaClient.NewListAvailablePager
//     method.
func (client *PTUQuotaClient) NewListAvailablePager(location string, options *PTUQuotaClientListAvailableOptions) *runtime.Pager[PTUQuotaClientListAvailableResponse] {
	return runtime.NewPager(runtime.PagingHandler[PTUQuotaClientListAvailableResponse]{
		More: func(page PTUQuotaClientListAvailableResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *PTUQuotaClientListAvailableResponse) (PTUQuotaClientListAvailableResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "PTUQuotaClient.NewListAvailablePager")
			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.listAvailableCreateRequest(ctx, location, options)
			}, nil)
			if err != nil {
				return PTUQuotaClientListAvailableResponse{}, err
			}
			return client.listAvailableHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listAvailableCreateRequest creates the ListAvailable request.
func (client *PTUQuotaClient) listAvailableCreateRequest(ctx context.Context, location string, options *PTUQuotaClientListAvailableOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.MachineLearningServices/locations/{location}/availableQuota"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if location == "" {
		return nil, errors.New("parameter location cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{location}", url.PathEscape(location))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	if options != nil && options.Skip != nil {
		reqQP.Set("$skip", *options.Skip)
	}
	reqQP.Set("api-version", version20260315Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listAvailableHandleResponse handles the ListAvailable response.
func (client *PTUQuotaClient) listAvailableHandleResponse(resp *http.Response) (PTUQuotaClientListAvailableResponse, error) {
	result := PTUQuotaClientListAvailableResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.AvailableQuotaArmPaginatedResult); err != nil {
		return PTUQuotaClientListAvailableResponse{}, err
	}
	return result, nil
}
