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

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

// FeaturesClient - Location-scoped operations for compute limit features.
// Don't use this type directly, use NewFeaturesClient() instead.
//
// Generated from API version 2026-07-31
type FeaturesClient struct {
	internal       *arm.Client
	subscriptionID string
}

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

// BeginDisable - Disables a compute limit feature for the subscription at the specified location. Requires the Contributor
// role.
// If the operation fails it returns an *azcore.ResponseError type.
//   - location - The name of the Azure region.
//   - featureName - The name of the Feature
//   - options - FeaturesClientBeginDisableOptions contains the optional parameters for the FeaturesClient.BeginDisable method.
func (client *FeaturesClient) BeginDisable(ctx context.Context, location string, featureName string, options *FeaturesClientBeginDisableOptions) (*runtime.Poller[FeaturesClientDisableResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.disable(ctx, location, featureName, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[FeaturesClientDisableResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[FeaturesClientDisableResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Disable - Disables a compute limit feature for the subscription at the specified location. Requires the Contributor role.
// If the operation fails it returns an *azcore.ResponseError type.
func (client *FeaturesClient) disable(ctx context.Context, location string, featureName string, options *FeaturesClientBeginDisableOptions) (*http.Response, error) {
	var err error
	const operationName = "FeaturesClient.BeginDisable"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.disableCreateRequest(ctx, location, featureName, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusAccepted) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// disableCreateRequest creates the Disable request.
func (client *FeaturesClient) disableCreateRequest(ctx context.Context, location string, featureName string, _ *FeaturesClientBeginDisableOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.ComputeLimit/locations/{location}/features/{featureName}/disable"
	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))
	if featureName == "" {
		return nil, errors.New("parameter featureName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{featureName}", url.PathEscape(featureName))
	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", version20260731)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// BeginEnable - Enables a compute limit feature for the subscription at the specified location. Requires the Contributor
// role.
// If the operation fails it returns an *azcore.ResponseError type.
//   - location - The name of the Azure region.
//   - featureName - The name of the Feature
//   - options - FeaturesClientBeginEnableOptions contains the optional parameters for the FeaturesClient.BeginEnable method.
func (client *FeaturesClient) BeginEnable(ctx context.Context, location string, featureName string, options *FeaturesClientBeginEnableOptions) (*runtime.Poller[FeaturesClientEnableResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.enable(ctx, location, featureName, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[FeaturesClientEnableResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[FeaturesClientEnableResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Enable - Enables a compute limit feature for the subscription at the specified location. Requires the Contributor role.
// If the operation fails it returns an *azcore.ResponseError type.
func (client *FeaturesClient) enable(ctx context.Context, location string, featureName string, options *FeaturesClientBeginEnableOptions) (*http.Response, error) {
	var err error
	const operationName = "FeaturesClient.BeginEnable"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.enableCreateRequest(ctx, location, featureName, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusAccepted) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// enableCreateRequest creates the Enable request.
func (client *FeaturesClient) enableCreateRequest(ctx context.Context, location string, featureName string, options *FeaturesClientBeginEnableOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.ComputeLimit/locations/{location}/features/{featureName}/enable"
	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))
	if featureName == "" {
		return nil, errors.New("parameter featureName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{featureName}", url.PathEscape(featureName))
	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", version20260731)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	if options != nil && options.Body != nil {
		req.Raw().Header["Content-Type"] = []string{"application/json"}
		if err := runtime.MarshalAsJSON(req, *options.Body); err != nil {
			return nil, err
		}
		return req, nil
	}
	return req, nil
}

// Get - Gets the properties of a compute limit feature.
// If the operation fails it returns an *azcore.ResponseError type.
//   - location - The name of the Azure region.
//   - featureName - The name of the Feature
//   - options - FeaturesClientGetOptions contains the optional parameters for the FeaturesClient.Get method.
func (client *FeaturesClient) Get(ctx context.Context, location string, featureName string, options *FeaturesClientGetOptions) (FeaturesClientGetResponse, error) {
	var err error
	const operationName = "FeaturesClient.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, location, featureName, options)
	if err != nil {
		return FeaturesClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return FeaturesClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return FeaturesClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *FeaturesClient) getCreateRequest(ctx context.Context, location string, featureName string, _ *FeaturesClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.ComputeLimit/locations/{location}/features/{featureName}"
	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))
	if featureName == "" {
		return nil, errors.New("parameter featureName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{featureName}", url.PathEscape(featureName))
	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", version20260731)
	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 *FeaturesClient) getHandleResponse(resp *http.Response) (FeaturesClientGetResponse, error) {
	result := FeaturesClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.Feature); err != nil {
		return FeaturesClientGetResponse{}, err
	}
	return result, nil
}

// NewListBySubscriptionLocationResourcePager - Lists all compute limit features for the subscription at the specified location.
//   - location - The name of the Azure region.
//   - options - FeaturesClientListBySubscriptionLocationResourceOptions contains the optional parameters for the FeaturesClient.NewListBySubscriptionLocationResourcePager
//     method.
func (client *FeaturesClient) NewListBySubscriptionLocationResourcePager(location string, options *FeaturesClientListBySubscriptionLocationResourceOptions) *runtime.Pager[FeaturesClientListBySubscriptionLocationResourceResponse] {
	return runtime.NewPager(runtime.PagingHandler[FeaturesClientListBySubscriptionLocationResourceResponse]{
		More: func(page FeaturesClientListBySubscriptionLocationResourceResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *FeaturesClientListBySubscriptionLocationResourceResponse) (FeaturesClientListBySubscriptionLocationResourceResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "FeaturesClient.NewListBySubscriptionLocationResourcePager")
			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.listBySubscriptionLocationResourceCreateRequest(ctx, location, options)
			}, nil)
			if err != nil {
				return FeaturesClientListBySubscriptionLocationResourceResponse{}, err
			}
			return client.listBySubscriptionLocationResourceHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listBySubscriptionLocationResourceCreateRequest creates the ListBySubscriptionLocationResource request.
func (client *FeaturesClient) listBySubscriptionLocationResourceCreateRequest(ctx context.Context, location string, _ *FeaturesClientListBySubscriptionLocationResourceOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.ComputeLimit/locations/{location}/features"
	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", version20260731)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listBySubscriptionLocationResourceHandleResponse handles the ListBySubscriptionLocationResource response.
func (client *FeaturesClient) listBySubscriptionLocationResourceHandleResponse(resp *http.Response) (FeaturesClientListBySubscriptionLocationResourceResponse, error) {
	result := FeaturesClientListBySubscriptionLocationResourceResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.FeatureListResult); err != nil {
		return FeaturesClientListBySubscriptionLocationResourceResponse{}, err
	}
	return result, nil
}
