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

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

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

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

// Get - Gets Spot Placement Scores metadata.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-06-05
//   - location - The name of the Azure region.
//   - options - SpotPlacementScoresClientGetOptions contains the optional parameters for the SpotPlacementScoresClient.Get method.
func (client *SpotPlacementScoresClient) Get(ctx context.Context, location string, options *SpotPlacementScoresClientGetOptions) (SpotPlacementScoresClientGetResponse, error) {
	var err error
	const operationName = "SpotPlacementScoresClient.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, options)
	if err != nil {
		return SpotPlacementScoresClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return SpotPlacementScoresClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return SpotPlacementScoresClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *SpotPlacementScoresClient) getCreateRequest(ctx context.Context, location string, _ *SpotPlacementScoresClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Compute/locations/{location}/placementScores/spot"
	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", "2025-06-05")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getHandleResponse handles the Get response.
func (client *SpotPlacementScoresClient) getHandleResponse(resp *http.Response) (SpotPlacementScoresClientGetResponse, error) {
	result := SpotPlacementScoresClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.ComputeDiagnosticBase); err != nil {
		return SpotPlacementScoresClientGetResponse{}, err
	}
	return result, nil
}

// Post - Generates placement scores for Spot VM skus.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-06-05
//   - location - The name of the Azure region.
//   - spotPlacementScoresInput - SpotPlacementScores object supplied in the body of the Post spot placement scores operation.
//   - options - SpotPlacementScoresClientPostOptions contains the optional parameters for the SpotPlacementScoresClient.Post
//     method.
func (client *SpotPlacementScoresClient) Post(ctx context.Context, location string, spotPlacementScoresInput SpotPlacementScoresInput, options *SpotPlacementScoresClientPostOptions) (SpotPlacementScoresClientPostResponse, error) {
	var err error
	const operationName = "SpotPlacementScoresClient.Post"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.postCreateRequest(ctx, location, spotPlacementScoresInput, options)
	if err != nil {
		return SpotPlacementScoresClientPostResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return SpotPlacementScoresClientPostResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return SpotPlacementScoresClientPostResponse{}, err
	}
	resp, err := client.postHandleResponse(httpResp)
	return resp, err
}

// postCreateRequest creates the Post request.
func (client *SpotPlacementScoresClient) postCreateRequest(ctx context.Context, location string, spotPlacementScoresInput SpotPlacementScoresInput, _ *SpotPlacementScoresClientPostOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Compute/locations/{location}/placementScores/spot/generate"
	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.MethodPost, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2025-06-05")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	req.Raw().Header["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, spotPlacementScoresInput); err != nil {
		return nil, err
	}
	return req, nil
}

// postHandleResponse handles the Post response.
func (client *SpotPlacementScoresClient) postHandleResponse(resp *http.Response) (SpotPlacementScoresClientPostResponse, error) {
	result := SpotPlacementScoresClientPostResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.SpotPlacementScoresResponse); err != nil {
		return SpotPlacementScoresClientPostResponse{}, err
	}
	return result, nil
}
