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

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

// ScoresClient contains the methods for the Scores group.
// Don't use this type directly, use NewScoresClient() instead.
//
// Generated from API version 2025-05-01-preview
type ScoresClient struct {
	internal       *arm.Client
	subscriptionID string
}

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

// Get - Gets the advisor score.
// If the operation fails it returns an *azcore.ResponseError type.
//   - name - The scope of Advisor score entity.
//   - options - ScoresClientGetOptions contains the optional parameters for the ScoresClient.Get method.
func (client *ScoresClient) Get(ctx context.Context, name string, options *ScoresClientGetOptions) (ScoresClientGetResponse, error) {
	var err error
	const operationName = "ScoresClient.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, name, options)
	if err != nil {
		return ScoresClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return ScoresClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return ScoresClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *ScoresClient) getCreateRequest(ctx context.Context, name string, _ *ScoresClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Advisor/advisorScore/{name}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if name == "" {
		return nil, errors.New("parameter name cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{name}", url.PathEscape(name))
	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", version20250501Preview)
	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 *ScoresClient) getHandleResponse(resp *http.Response) (ScoresClientGetResponse, error) {
	result := ScoresClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ScoreEntityForAdvisor); err != nil {
		return ScoresClientGetResponse{}, err
	}
	return result, nil
}

// NewListPager - Gets the list of advisor scores.
//   - options - ScoresClientListOptions contains the optional parameters for the ScoresClient.NewListPager method.
func (client *ScoresClient) NewListPager(options *ScoresClientListOptions) *runtime.Pager[ScoresClientListResponse] {
	return runtime.NewPager(runtime.PagingHandler[ScoresClientListResponse]{
		More: func(page ScoresClientListResponse) bool {
			return false
		},
		Fetcher: func(ctx context.Context, page *ScoresClientListResponse) (ScoresClientListResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "ScoresClient.NewListPager")
			req, err := client.listCreateRequest(ctx, options)
			if err != nil {
				return ScoresClientListResponse{}, err
			}
			resp, err := client.internal.Pipeline().Do(req)
			if err != nil {
				return ScoresClientListResponse{}, err
			}
			if !runtime.HasStatusCode(resp, http.StatusOK) {
				return ScoresClientListResponse{}, runtime.NewResponseError(resp)
			}
			return client.listHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listCreateRequest creates the List request.
func (client *ScoresClient) listCreateRequest(ctx context.Context, _ *ScoresClientListOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Advisor/advisorScore"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	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", version20250501Preview)
	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 *ScoresClient) listHandleResponse(resp *http.Response) (ScoresClientListResponse, error) {
	result := ScoresClientListResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ScoreResponse); err != nil {
		return ScoresClientListResponse{}, err
	}
	return result, nil
}
