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

import (
	"context"
	"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"
)

// CarbonServiceClient contains the methods for the CarbonService group.
// Don't use this type directly, use NewCarbonServiceClient() instead.
type CarbonServiceClient struct {
	internal *arm.Client
}

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

// QueryCarbonEmissionDataAvailableDateRange - API for query carbon emission data available date range
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-04-01
//   - options - CarbonServiceClientQueryCarbonEmissionDataAvailableDateRangeOptions contains the optional parameters for the
//     CarbonServiceClient.QueryCarbonEmissionDataAvailableDateRange method.
func (client *CarbonServiceClient) QueryCarbonEmissionDataAvailableDateRange(ctx context.Context, options *CarbonServiceClientQueryCarbonEmissionDataAvailableDateRangeOptions) (CarbonServiceClientQueryCarbonEmissionDataAvailableDateRangeResponse, error) {
	var err error
	const operationName = "CarbonServiceClient.QueryCarbonEmissionDataAvailableDateRange"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.queryCarbonEmissionDataAvailableDateRangeCreateRequest(ctx, options)
	if err != nil {
		return CarbonServiceClientQueryCarbonEmissionDataAvailableDateRangeResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return CarbonServiceClientQueryCarbonEmissionDataAvailableDateRangeResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return CarbonServiceClientQueryCarbonEmissionDataAvailableDateRangeResponse{}, err
	}
	resp, err := client.queryCarbonEmissionDataAvailableDateRangeHandleResponse(httpResp)
	return resp, err
}

// queryCarbonEmissionDataAvailableDateRangeCreateRequest creates the QueryCarbonEmissionDataAvailableDateRange request.
func (client *CarbonServiceClient) queryCarbonEmissionDataAvailableDateRangeCreateRequest(ctx context.Context, _ *CarbonServiceClientQueryCarbonEmissionDataAvailableDateRangeOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Carbon/queryCarbonEmissionDataAvailableDateRange"
	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-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// queryCarbonEmissionDataAvailableDateRangeHandleResponse handles the QueryCarbonEmissionDataAvailableDateRange response.
func (client *CarbonServiceClient) queryCarbonEmissionDataAvailableDateRangeHandleResponse(resp *http.Response) (CarbonServiceClientQueryCarbonEmissionDataAvailableDateRangeResponse, error) {
	result := CarbonServiceClientQueryCarbonEmissionDataAvailableDateRangeResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.CarbonEmissionDataAvailableDateRange); err != nil {
		return CarbonServiceClientQueryCarbonEmissionDataAvailableDateRangeResponse{}, err
	}
	return result, nil
}

// QueryCarbonEmissionReports - API for Carbon Emissions Reports
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-04-01
//   - queryParameters - Query parameters
//   - options - CarbonServiceClientQueryCarbonEmissionReportsOptions contains the optional parameters for the CarbonServiceClient.QueryCarbonEmissionReports
//     method.
func (client *CarbonServiceClient) QueryCarbonEmissionReports(ctx context.Context, queryParameters QueryFilterClassification, options *CarbonServiceClientQueryCarbonEmissionReportsOptions) (CarbonServiceClientQueryCarbonEmissionReportsResponse, error) {
	var err error
	const operationName = "CarbonServiceClient.QueryCarbonEmissionReports"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.queryCarbonEmissionReportsCreateRequest(ctx, queryParameters, options)
	if err != nil {
		return CarbonServiceClientQueryCarbonEmissionReportsResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return CarbonServiceClientQueryCarbonEmissionReportsResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return CarbonServiceClientQueryCarbonEmissionReportsResponse{}, err
	}
	resp, err := client.queryCarbonEmissionReportsHandleResponse(httpResp)
	return resp, err
}

// queryCarbonEmissionReportsCreateRequest creates the QueryCarbonEmissionReports request.
func (client *CarbonServiceClient) queryCarbonEmissionReportsCreateRequest(ctx context.Context, queryParameters QueryFilterClassification, _ *CarbonServiceClientQueryCarbonEmissionReportsOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Carbon/carbonEmissionReports"
	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-04-01")
	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, queryParameters); err != nil {
		return nil, err
	}
	return req, nil
}

// queryCarbonEmissionReportsHandleResponse handles the QueryCarbonEmissionReports response.
func (client *CarbonServiceClient) queryCarbonEmissionReportsHandleResponse(resp *http.Response) (CarbonServiceClientQueryCarbonEmissionReportsResponse, error) {
	result := CarbonServiceClientQueryCarbonEmissionReportsResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.CarbonEmissionDataListResult); err != nil {
		return CarbonServiceClientQueryCarbonEmissionReportsResponse{}, err
	}
	return result, nil
}
