// 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) AutoRest Code Generator. DO NOT EDIT.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

package armpostgresqlflexibleservers

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

// TuningIndexClient contains the methods for the TuningIndex group.
// Don't use this type directly, use NewTuningIndexClient() instead.
type TuningIndexClient struct {
	internal       *arm.Client
	subscriptionID string
}

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

// NewListRecommendationsPager - Retrieve the list of available tuning index recommendations.
//
// Generated from API version 2025-01-01-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - serverName - The name of the server.
//   - tuningOption - The name of the tuning option.
//   - options - TuningIndexClientListRecommendationsOptions contains the optional parameters for the TuningIndexClient.NewListRecommendationsPager
//     method.
func (client *TuningIndexClient) NewListRecommendationsPager(resourceGroupName string, serverName string, tuningOption TuningOptionEnum, options *TuningIndexClientListRecommendationsOptions) *runtime.Pager[TuningIndexClientListRecommendationsResponse] {
	return runtime.NewPager(runtime.PagingHandler[TuningIndexClientListRecommendationsResponse]{
		More: func(page TuningIndexClientListRecommendationsResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *TuningIndexClientListRecommendationsResponse) (TuningIndexClientListRecommendationsResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "TuningIndexClient.NewListRecommendationsPager")
			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.listRecommendationsCreateRequest(ctx, resourceGroupName, serverName, tuningOption, options)
			}, nil)
			if err != nil {
				return TuningIndexClientListRecommendationsResponse{}, err
			}
			return client.listRecommendationsHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listRecommendationsCreateRequest creates the ListRecommendations request.
func (client *TuningIndexClient) listRecommendationsCreateRequest(ctx context.Context, resourceGroupName string, serverName string, tuningOption TuningOptionEnum, options *TuningIndexClientListRecommendationsOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforPostgreSQL/flexibleServers/{serverName}/tuningOptions/{tuningOption}/recommendations"
	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 serverName == "" {
		return nil, errors.New("parameter serverName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{serverName}", url.PathEscape(serverName))
	if tuningOption == "" {
		return nil, errors.New("parameter tuningOption cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{tuningOption}", url.PathEscape(string(tuningOption)))
	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-01-01-preview")
	if options != nil && options.RecommendationType != nil {
		reqQP.Set("recommendationType", string(*options.RecommendationType))
	}
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listRecommendationsHandleResponse handles the ListRecommendations response.
func (client *TuningIndexClient) listRecommendationsHandleResponse(resp *http.Response) (TuningIndexClientListRecommendationsResponse, error) {
	result := TuningIndexClientListRecommendationsResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.IndexRecommendationListResult); err != nil {
		return TuningIndexClientListRecommendationsResponse{}, err
	}
	return result, nil
}
