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

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

// CreditsClient contains the methods for the Credits group.
// Don't use this type directly, use NewCreditsClient() instead.
//
// Generated from API version 2024-08-01
type CreditsClient struct {
	internal *arm.Client
}

// NewCreditsClient creates a new instance of CreditsClient with the specified values.
//   - credential - used to authorize requests. Usually a credential from azidentity.
//   - options - Contains optional client configuration. Pass nil to accept the default values.
func NewCreditsClient(credential azcore.TokenCredential, options *arm.ClientOptions) (*CreditsClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &CreditsClient{
		internal: cl,
	}
	return client, nil
}

// Get - The credit summary by billingAccountId and billingProfileId.
// If the operation fails it returns an *azcore.ResponseError type.
//   - billingAccountID - BillingAccount ID
//   - billingProfileID - Azure Billing Profile ID.
//   - options - CreditsClientGetOptions contains the optional parameters for the CreditsClient.Get method.
func (client *CreditsClient) Get(ctx context.Context, billingAccountID string, billingProfileID string, options *CreditsClientGetOptions) (CreditsClientGetResponse, error) {
	var err error
	const operationName = "CreditsClient.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, billingAccountID, billingProfileID, options)
	if err != nil {
		return CreditsClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return CreditsClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusNoContent) {
		err = runtime.NewResponseError(httpResp)
		return CreditsClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *CreditsClient) getCreateRequest(ctx context.Context, billingAccountID string, billingProfileID string, _ *CreditsClientGetOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Billing/billingAccounts/{billingAccountId}/billingProfiles/{billingProfileId}/providers/Microsoft.Consumption/credits/balanceSummary"
	if billingAccountID == "" {
		return nil, errors.New("parameter billingAccountID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{billingAccountId}", url.PathEscape(billingAccountID))
	if billingProfileID == "" {
		return nil, errors.New("parameter billingProfileID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{billingProfileId}", url.PathEscape(billingProfileID))
	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", version20240801)
	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 *CreditsClient) getHandleResponse(resp *http.Response) (CreditsClientGetResponse, error) {
	result := CreditsClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.CreditSummary); err != nil {
		return CreditsClientGetResponse{}, err
	}
	return result, nil
}
