//go:build go1.18
// +build go1.18

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

import (
	"context"
	"errors"
	"net/http"
	"net/url"
	"strconv"
	"strings"

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

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

// NewInvoicesClient creates a new instance of InvoicesClient with the specified values.
//   - subscriptionID - The ID that uniquely identifies a billing subscription.
//   - credential - used to authorize requests. Usually a credential from azidentity.
//   - options - pass nil to accept the default values.
func NewInvoicesClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*InvoicesClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &InvoicesClient{
		subscriptionID: subscriptionID,
		internal:       cl,
	}
	return client, nil
}

// BeginAmend - Regenerate an invoice by billing account name and invoice name. The operation is supported for billing accounts
// with agreement type Microsoft Customer Agreement.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-04-01
//   - billingAccountName - The ID that uniquely identifies a billing account.
//   - invoiceName - The ID that uniquely identifies an invoice.
//   - options - InvoicesClientBeginAmendOptions contains the optional parameters for the InvoicesClient.BeginAmend method.
func (client *InvoicesClient) BeginAmend(ctx context.Context, billingAccountName string, invoiceName string, options *InvoicesClientBeginAmendOptions) (*runtime.Poller[InvoicesClientAmendResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.amend(ctx, billingAccountName, invoiceName, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[InvoicesClientAmendResponse]{
			FinalStateVia: runtime.FinalStateViaLocation,
			Tracer:        client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[InvoicesClientAmendResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Amend - Regenerate an invoice by billing account name and invoice name. The operation is supported for billing accounts
// with agreement type Microsoft Customer Agreement.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-04-01
func (client *InvoicesClient) amend(ctx context.Context, billingAccountName string, invoiceName string, options *InvoicesClientBeginAmendOptions) (*http.Response, error) {
	var err error
	const operationName = "InvoicesClient.BeginAmend"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.amendCreateRequest(ctx, billingAccountName, invoiceName, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusAccepted) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// amendCreateRequest creates the Amend request.
func (client *InvoicesClient) amendCreateRequest(ctx context.Context, billingAccountName string, invoiceName string, options *InvoicesClientBeginAmendOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Billing/billingAccounts/{billingAccountName}/invoices/{invoiceName}/amend"
	if billingAccountName == "" {
		return nil, errors.New("parameter billingAccountName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{billingAccountName}", url.PathEscape(billingAccountName))
	if invoiceName == "" {
		return nil, errors.New("parameter invoiceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{invoiceName}", url.PathEscape(invoiceName))
	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", "2024-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// BeginDownloadByBillingAccount - Gets a URL to download an invoice document. The operation is supported for billing accounts
// with agreement type Microsoft Partner Agreement, Microsoft Customer Agreement or Enterprise Agreement.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-04-01
//   - billingAccountName - The ID that uniquely identifies a billing account.
//   - invoiceName - The ID that uniquely identifies an invoice.
//   - options - InvoicesClientBeginDownloadByBillingAccountOptions contains the optional parameters for the InvoicesClient.BeginDownloadByBillingAccount
//     method.
func (client *InvoicesClient) BeginDownloadByBillingAccount(ctx context.Context, billingAccountName string, invoiceName string, options *InvoicesClientBeginDownloadByBillingAccountOptions) (*runtime.Poller[InvoicesClientDownloadByBillingAccountResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.downloadByBillingAccount(ctx, billingAccountName, invoiceName, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[InvoicesClientDownloadByBillingAccountResponse]{
			FinalStateVia: runtime.FinalStateViaLocation,
			Tracer:        client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[InvoicesClientDownloadByBillingAccountResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// DownloadByBillingAccount - Gets a URL to download an invoice document. The operation is supported for billing accounts
// with agreement type Microsoft Partner Agreement, Microsoft Customer Agreement or Enterprise Agreement.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-04-01
func (client *InvoicesClient) downloadByBillingAccount(ctx context.Context, billingAccountName string, invoiceName string, options *InvoicesClientBeginDownloadByBillingAccountOptions) (*http.Response, error) {
	var err error
	const operationName = "InvoicesClient.BeginDownloadByBillingAccount"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.downloadByBillingAccountCreateRequest(ctx, billingAccountName, invoiceName, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusAccepted) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// downloadByBillingAccountCreateRequest creates the DownloadByBillingAccount request.
func (client *InvoicesClient) downloadByBillingAccountCreateRequest(ctx context.Context, billingAccountName string, invoiceName string, options *InvoicesClientBeginDownloadByBillingAccountOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Billing/billingAccounts/{billingAccountName}/invoices/{invoiceName}/download"
	if billingAccountName == "" {
		return nil, errors.New("parameter billingAccountName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{billingAccountName}", url.PathEscape(billingAccountName))
	if invoiceName == "" {
		return nil, errors.New("parameter invoiceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{invoiceName}", url.PathEscape(invoiceName))
	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", "2024-04-01")
	if options != nil && options.DocumentName != nil {
		reqQP.Set("documentName", *options.DocumentName)
	}
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// BeginDownloadByBillingSubscription - Gets a URL to download an invoice by billing subscription. The operation is supported
// for billing accounts with agreement type Microsoft Partner Agreement or Microsoft Customer Agreement.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-04-01
//   - invoiceName - The ID that uniquely identifies an invoice.
//   - options - InvoicesClientBeginDownloadByBillingSubscriptionOptions contains the optional parameters for the InvoicesClient.BeginDownloadByBillingSubscription
//     method.
func (client *InvoicesClient) BeginDownloadByBillingSubscription(ctx context.Context, invoiceName string, options *InvoicesClientBeginDownloadByBillingSubscriptionOptions) (*runtime.Poller[InvoicesClientDownloadByBillingSubscriptionResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.downloadByBillingSubscription(ctx, invoiceName, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[InvoicesClientDownloadByBillingSubscriptionResponse]{
			FinalStateVia: runtime.FinalStateViaLocation,
			Tracer:        client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[InvoicesClientDownloadByBillingSubscriptionResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// DownloadByBillingSubscription - Gets a URL to download an invoice by billing subscription. The operation is supported for
// billing accounts with agreement type Microsoft Partner Agreement or Microsoft Customer Agreement.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-04-01
func (client *InvoicesClient) downloadByBillingSubscription(ctx context.Context, invoiceName string, options *InvoicesClientBeginDownloadByBillingSubscriptionOptions) (*http.Response, error) {
	var err error
	const operationName = "InvoicesClient.BeginDownloadByBillingSubscription"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.downloadByBillingSubscriptionCreateRequest(ctx, invoiceName, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusAccepted) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// downloadByBillingSubscriptionCreateRequest creates the DownloadByBillingSubscription request.
func (client *InvoicesClient) downloadByBillingSubscriptionCreateRequest(ctx context.Context, invoiceName string, options *InvoicesClientBeginDownloadByBillingSubscriptionOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Billing/billingAccounts/default/billingSubscriptions/{subscriptionId}/invoices/{invoiceName}/download"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if invoiceName == "" {
		return nil, errors.New("parameter invoiceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{invoiceName}", url.PathEscape(invoiceName))
	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", "2024-04-01")
	if options != nil && options.DocumentName != nil {
		reqQP.Set("documentName", *options.DocumentName)
	}
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// BeginDownloadDocumentsByBillingAccount - Gets a URL to download multiple invoice documents (invoice pdf, tax receipts,
// credit notes) as a zip file. The operation is supported for billing accounts with agreement type Microsoft Partner
// Agreement or Microsoft Customer Agreement.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-04-01
//   - billingAccountName - The ID that uniquely identifies a billing account.
//   - parameters - A list of download details for individual documents.
//   - options - InvoicesClientBeginDownloadDocumentsByBillingAccountOptions contains the optional parameters for the InvoicesClient.BeginDownloadDocumentsByBillingAccount
//     method.
func (client *InvoicesClient) BeginDownloadDocumentsByBillingAccount(ctx context.Context, billingAccountName string, parameters []*DocumentDownloadRequest, options *InvoicesClientBeginDownloadDocumentsByBillingAccountOptions) (*runtime.Poller[InvoicesClientDownloadDocumentsByBillingAccountResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.downloadDocumentsByBillingAccount(ctx, billingAccountName, parameters, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[InvoicesClientDownloadDocumentsByBillingAccountResponse]{
			FinalStateVia: runtime.FinalStateViaLocation,
			Tracer:        client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[InvoicesClientDownloadDocumentsByBillingAccountResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// DownloadDocumentsByBillingAccount - Gets a URL to download multiple invoice documents (invoice pdf, tax receipts, credit
// notes) as a zip file. The operation is supported for billing accounts with agreement type Microsoft Partner
// Agreement or Microsoft Customer Agreement.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-04-01
func (client *InvoicesClient) downloadDocumentsByBillingAccount(ctx context.Context, billingAccountName string, parameters []*DocumentDownloadRequest, options *InvoicesClientBeginDownloadDocumentsByBillingAccountOptions) (*http.Response, error) {
	var err error
	const operationName = "InvoicesClient.BeginDownloadDocumentsByBillingAccount"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.downloadDocumentsByBillingAccountCreateRequest(ctx, billingAccountName, parameters, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusAccepted) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// downloadDocumentsByBillingAccountCreateRequest creates the DownloadDocumentsByBillingAccount request.
func (client *InvoicesClient) downloadDocumentsByBillingAccountCreateRequest(ctx context.Context, billingAccountName string, parameters []*DocumentDownloadRequest, options *InvoicesClientBeginDownloadDocumentsByBillingAccountOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Billing/billingAccounts/{billingAccountName}/downloadDocuments"
	if billingAccountName == "" {
		return nil, errors.New("parameter billingAccountName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{billingAccountName}", url.PathEscape(billingAccountName))
	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", "2024-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, parameters); err != nil {
		return nil, err
	}
	return req, nil
}

// BeginDownloadDocumentsByBillingSubscription - Gets a URL to download multiple invoice documents (invoice pdf, tax receipts,
// credit notes) as a zip file. The operation is supported for billing accounts with agreement type Microsoft Partner
// Agreement or Microsoft Customer Agreement.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-04-01
//   - parameters - A list of download details for individual documents.
//   - options - InvoicesClientBeginDownloadDocumentsByBillingSubscriptionOptions contains the optional parameters for the InvoicesClient.BeginDownloadDocumentsByBillingSubscription
//     method.
func (client *InvoicesClient) BeginDownloadDocumentsByBillingSubscription(ctx context.Context, parameters []*DocumentDownloadRequest, options *InvoicesClientBeginDownloadDocumentsByBillingSubscriptionOptions) (*runtime.Poller[InvoicesClientDownloadDocumentsByBillingSubscriptionResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.downloadDocumentsByBillingSubscription(ctx, parameters, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[InvoicesClientDownloadDocumentsByBillingSubscriptionResponse]{
			FinalStateVia: runtime.FinalStateViaLocation,
			Tracer:        client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[InvoicesClientDownloadDocumentsByBillingSubscriptionResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// DownloadDocumentsByBillingSubscription - Gets a URL to download multiple invoice documents (invoice pdf, tax receipts,
// credit notes) as a zip file. The operation is supported for billing accounts with agreement type Microsoft Partner
// Agreement or Microsoft Customer Agreement.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-04-01
func (client *InvoicesClient) downloadDocumentsByBillingSubscription(ctx context.Context, parameters []*DocumentDownloadRequest, options *InvoicesClientBeginDownloadDocumentsByBillingSubscriptionOptions) (*http.Response, error) {
	var err error
	const operationName = "InvoicesClient.BeginDownloadDocumentsByBillingSubscription"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.downloadDocumentsByBillingSubscriptionCreateRequest(ctx, parameters, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusAccepted) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// downloadDocumentsByBillingSubscriptionCreateRequest creates the DownloadDocumentsByBillingSubscription request.
func (client *InvoicesClient) downloadDocumentsByBillingSubscriptionCreateRequest(ctx context.Context, parameters []*DocumentDownloadRequest, options *InvoicesClientBeginDownloadDocumentsByBillingSubscriptionOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Billing/billingAccounts/default/billingSubscriptions/{subscriptionId}/downloadDocuments"
	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.MethodPost, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2024-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, parameters); err != nil {
		return nil, err
	}
	return req, nil
}

// BeginDownloadSummaryByBillingAccount - Gets a URL to download the summary document for an invoice. The operation is supported
// for billing accounts with agreement type Enterprise Agreement.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-04-01
//   - billingAccountName - The ID that uniquely identifies a billing account.
//   - invoiceName - The ID that uniquely identifies an invoice.
//   - options - InvoicesClientBeginDownloadSummaryByBillingAccountOptions contains the optional parameters for the InvoicesClient.BeginDownloadSummaryByBillingAccount
//     method.
func (client *InvoicesClient) BeginDownloadSummaryByBillingAccount(ctx context.Context, billingAccountName string, invoiceName string, options *InvoicesClientBeginDownloadSummaryByBillingAccountOptions) (*runtime.Poller[InvoicesClientDownloadSummaryByBillingAccountResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.downloadSummaryByBillingAccount(ctx, billingAccountName, invoiceName, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[InvoicesClientDownloadSummaryByBillingAccountResponse]{
			FinalStateVia: runtime.FinalStateViaLocation,
			Tracer:        client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[InvoicesClientDownloadSummaryByBillingAccountResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// DownloadSummaryByBillingAccount - Gets a URL to download the summary document for an invoice. The operation is supported
// for billing accounts with agreement type Enterprise Agreement.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-04-01
func (client *InvoicesClient) downloadSummaryByBillingAccount(ctx context.Context, billingAccountName string, invoiceName string, options *InvoicesClientBeginDownloadSummaryByBillingAccountOptions) (*http.Response, error) {
	var err error
	const operationName = "InvoicesClient.BeginDownloadSummaryByBillingAccount"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.downloadSummaryByBillingAccountCreateRequest(ctx, billingAccountName, invoiceName, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusAccepted) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// downloadSummaryByBillingAccountCreateRequest creates the DownloadSummaryByBillingAccount request.
func (client *InvoicesClient) downloadSummaryByBillingAccountCreateRequest(ctx context.Context, billingAccountName string, invoiceName string, options *InvoicesClientBeginDownloadSummaryByBillingAccountOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Billing/billingAccounts/{billingAccountName}/invoices/{invoiceName}/downloadSummary"
	if billingAccountName == "" {
		return nil, errors.New("parameter billingAccountName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{billingAccountName}", url.PathEscape(billingAccountName))
	if invoiceName == "" {
		return nil, errors.New("parameter invoiceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{invoiceName}", url.PathEscape(invoiceName))
	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", "2024-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// Get - Gets an invoice by ID. The operation is supported for billing accounts with agreement type Microsoft Partner Agreement
// or Microsoft Customer Agreement.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-04-01
//   - invoiceName - The ID that uniquely identifies an invoice.
//   - options - InvoicesClientGetOptions contains the optional parameters for the InvoicesClient.Get method.
func (client *InvoicesClient) Get(ctx context.Context, invoiceName string, options *InvoicesClientGetOptions) (InvoicesClientGetResponse, error) {
	var err error
	const operationName = "InvoicesClient.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, invoiceName, options)
	if err != nil {
		return InvoicesClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return InvoicesClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return InvoicesClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *InvoicesClient) getCreateRequest(ctx context.Context, invoiceName string, options *InvoicesClientGetOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Billing/billingAccounts/default/invoices/{invoiceName}"
	if invoiceName == "" {
		return nil, errors.New("parameter invoiceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{invoiceName}", url.PathEscape(invoiceName))
	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", "2024-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// GetByBillingAccount - Gets an invoice by billing account name and ID. The operation is supported for all billing account
// types.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-04-01
//   - billingAccountName - The ID that uniquely identifies a billing account.
//   - invoiceName - The ID that uniquely identifies an invoice.
//   - options - InvoicesClientGetByBillingAccountOptions contains the optional parameters for the InvoicesClient.GetByBillingAccount
//     method.
func (client *InvoicesClient) GetByBillingAccount(ctx context.Context, billingAccountName string, invoiceName string, options *InvoicesClientGetByBillingAccountOptions) (InvoicesClientGetByBillingAccountResponse, error) {
	var err error
	const operationName = "InvoicesClient.GetByBillingAccount"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.getByBillingAccountCreateRequest(ctx, billingAccountName, invoiceName, options)
	if err != nil {
		return InvoicesClientGetByBillingAccountResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return InvoicesClientGetByBillingAccountResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return InvoicesClientGetByBillingAccountResponse{}, err
	}
	resp, err := client.getByBillingAccountHandleResponse(httpResp)
	return resp, err
}

// getByBillingAccountCreateRequest creates the GetByBillingAccount request.
func (client *InvoicesClient) getByBillingAccountCreateRequest(ctx context.Context, billingAccountName string, invoiceName string, options *InvoicesClientGetByBillingAccountOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Billing/billingAccounts/{billingAccountName}/invoices/{invoiceName}"
	if billingAccountName == "" {
		return nil, errors.New("parameter billingAccountName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{billingAccountName}", url.PathEscape(billingAccountName))
	if invoiceName == "" {
		return nil, errors.New("parameter invoiceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{invoiceName}", url.PathEscape(invoiceName))
	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", "2024-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getByBillingAccountHandleResponse handles the GetByBillingAccount response.
func (client *InvoicesClient) getByBillingAccountHandleResponse(resp *http.Response) (InvoicesClientGetByBillingAccountResponse, error) {
	result := InvoicesClientGetByBillingAccountResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.Invoice); err != nil {
		return InvoicesClientGetByBillingAccountResponse{}, err
	}
	return result, nil
}

// GetByBillingSubscription - Gets an invoice by subscription ID and invoice ID. The operation is supported for billing accounts
// with agreement type Microsoft Partner Agreement or Microsoft Customer Agreement.
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2024-04-01
//   - invoiceName - The ID that uniquely identifies an invoice.
//   - options - InvoicesClientGetByBillingSubscriptionOptions contains the optional parameters for the InvoicesClient.GetByBillingSubscription
//     method.
func (client *InvoicesClient) GetByBillingSubscription(ctx context.Context, invoiceName string, options *InvoicesClientGetByBillingSubscriptionOptions) (InvoicesClientGetByBillingSubscriptionResponse, error) {
	var err error
	const operationName = "InvoicesClient.GetByBillingSubscription"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.getByBillingSubscriptionCreateRequest(ctx, invoiceName, options)
	if err != nil {
		return InvoicesClientGetByBillingSubscriptionResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return InvoicesClientGetByBillingSubscriptionResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return InvoicesClientGetByBillingSubscriptionResponse{}, err
	}
	resp, err := client.getByBillingSubscriptionHandleResponse(httpResp)
	return resp, err
}

// getByBillingSubscriptionCreateRequest creates the GetByBillingSubscription request.
func (client *InvoicesClient) getByBillingSubscriptionCreateRequest(ctx context.Context, invoiceName string, options *InvoicesClientGetByBillingSubscriptionOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Billing/billingAccounts/default/billingSubscriptions/{subscriptionId}/invoices/{invoiceName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if invoiceName == "" {
		return nil, errors.New("parameter invoiceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{invoiceName}", url.PathEscape(invoiceName))
	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", "2024-04-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getByBillingSubscriptionHandleResponse handles the GetByBillingSubscription response.
func (client *InvoicesClient) getByBillingSubscriptionHandleResponse(resp *http.Response) (InvoicesClientGetByBillingSubscriptionResponse, error) {
	result := InvoicesClientGetByBillingSubscriptionResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.Invoice); err != nil {
		return InvoicesClientGetByBillingSubscriptionResponse{}, err
	}
	return result, nil
}

// NewListByBillingAccountPager - Lists the invoices for a billing account for a given start date and end date. The operation
// is supported for all billing account types.
//
// Generated from API version 2024-04-01
//   - billingAccountName - The ID that uniquely identifies a billing account.
//   - options - InvoicesClientListByBillingAccountOptions contains the optional parameters for the InvoicesClient.NewListByBillingAccountPager
//     method.
func (client *InvoicesClient) NewListByBillingAccountPager(billingAccountName string, options *InvoicesClientListByBillingAccountOptions) *runtime.Pager[InvoicesClientListByBillingAccountResponse] {
	return runtime.NewPager(runtime.PagingHandler[InvoicesClientListByBillingAccountResponse]{
		More: func(page InvoicesClientListByBillingAccountResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *InvoicesClientListByBillingAccountResponse) (InvoicesClientListByBillingAccountResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "InvoicesClient.NewListByBillingAccountPager")
			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.listByBillingAccountCreateRequest(ctx, billingAccountName, options)
			}, nil)
			if err != nil {
				return InvoicesClientListByBillingAccountResponse{}, err
			}
			return client.listByBillingAccountHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByBillingAccountCreateRequest creates the ListByBillingAccount request.
func (client *InvoicesClient) listByBillingAccountCreateRequest(ctx context.Context, billingAccountName string, options *InvoicesClientListByBillingAccountOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Billing/billingAccounts/{billingAccountName}/invoices"
	if billingAccountName == "" {
		return nil, errors.New("parameter billingAccountName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{billingAccountName}", url.PathEscape(billingAccountName))
	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", "2024-04-01")
	if options != nil && options.Count != nil {
		reqQP.Set("count", strconv.FormatBool(*options.Count))
	}
	if options != nil && options.Filter != nil {
		reqQP.Set("filter", *options.Filter)
	}
	if options != nil && options.OrderBy != nil {
		reqQP.Set("orderBy", *options.OrderBy)
	}
	if options != nil && options.PeriodEndDate != nil {
		reqQP.Set("periodEndDate", options.PeriodEndDate.Format("2006-01-02"))
	}
	if options != nil && options.PeriodStartDate != nil {
		reqQP.Set("periodStartDate", options.PeriodStartDate.Format("2006-01-02"))
	}
	if options != nil && options.Search != nil {
		reqQP.Set("search", *options.Search)
	}
	if options != nil && options.Skip != nil {
		reqQP.Set("skip", strconv.FormatInt(*options.Skip, 10))
	}
	if options != nil && options.Top != nil {
		reqQP.Set("top", strconv.FormatInt(*options.Top, 10))
	}
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByBillingAccountHandleResponse handles the ListByBillingAccount response.
func (client *InvoicesClient) listByBillingAccountHandleResponse(resp *http.Response) (InvoicesClientListByBillingAccountResponse, error) {
	result := InvoicesClientListByBillingAccountResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.InvoiceListResult); err != nil {
		return InvoicesClientListByBillingAccountResponse{}, err
	}
	return result, nil
}

// NewListByBillingProfilePager - Lists the invoices for a billing profile for a given start date and end date. The operation
// is supported for billing accounts with agreement type Microsoft Partner Agreement or Microsoft Customer
// Agreement.
//
// Generated from API version 2024-04-01
//   - billingAccountName - The ID that uniquely identifies a billing account.
//   - billingProfileName - The ID that uniquely identifies a billing profile.
//   - options - InvoicesClientListByBillingProfileOptions contains the optional parameters for the InvoicesClient.NewListByBillingProfilePager
//     method.
func (client *InvoicesClient) NewListByBillingProfilePager(billingAccountName string, billingProfileName string, options *InvoicesClientListByBillingProfileOptions) *runtime.Pager[InvoicesClientListByBillingProfileResponse] {
	return runtime.NewPager(runtime.PagingHandler[InvoicesClientListByBillingProfileResponse]{
		More: func(page InvoicesClientListByBillingProfileResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *InvoicesClientListByBillingProfileResponse) (InvoicesClientListByBillingProfileResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "InvoicesClient.NewListByBillingProfilePager")
			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.listByBillingProfileCreateRequest(ctx, billingAccountName, billingProfileName, options)
			}, nil)
			if err != nil {
				return InvoicesClientListByBillingProfileResponse{}, err
			}
			return client.listByBillingProfileHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByBillingProfileCreateRequest creates the ListByBillingProfile request.
func (client *InvoicesClient) listByBillingProfileCreateRequest(ctx context.Context, billingAccountName string, billingProfileName string, options *InvoicesClientListByBillingProfileOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Billing/billingAccounts/{billingAccountName}/billingProfiles/{billingProfileName}/invoices"
	if billingAccountName == "" {
		return nil, errors.New("parameter billingAccountName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{billingAccountName}", url.PathEscape(billingAccountName))
	if billingProfileName == "" {
		return nil, errors.New("parameter billingProfileName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{billingProfileName}", url.PathEscape(billingProfileName))
	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", "2024-04-01")
	if options != nil && options.Count != nil {
		reqQP.Set("count", strconv.FormatBool(*options.Count))
	}
	if options != nil && options.Filter != nil {
		reqQP.Set("filter", *options.Filter)
	}
	if options != nil && options.OrderBy != nil {
		reqQP.Set("orderBy", *options.OrderBy)
	}
	if options != nil && options.PeriodEndDate != nil {
		reqQP.Set("periodEndDate", options.PeriodEndDate.Format("2006-01-02"))
	}
	if options != nil && options.PeriodStartDate != nil {
		reqQP.Set("periodStartDate", options.PeriodStartDate.Format("2006-01-02"))
	}
	if options != nil && options.Search != nil {
		reqQP.Set("search", *options.Search)
	}
	if options != nil && options.Skip != nil {
		reqQP.Set("skip", strconv.FormatInt(*options.Skip, 10))
	}
	if options != nil && options.Top != nil {
		reqQP.Set("top", strconv.FormatInt(*options.Top, 10))
	}
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByBillingProfileHandleResponse handles the ListByBillingProfile response.
func (client *InvoicesClient) listByBillingProfileHandleResponse(resp *http.Response) (InvoicesClientListByBillingProfileResponse, error) {
	result := InvoicesClientListByBillingProfileResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.InvoiceListResult); err != nil {
		return InvoicesClientListByBillingProfileResponse{}, err
	}
	return result, nil
}

// NewListByBillingSubscriptionPager - Lists the invoices for a subscription. The operation is supported for billing accounts
// with agreement type Microsoft Partner Agreement or Microsoft Customer Agreement.
//
// Generated from API version 2024-04-01
//   - options - InvoicesClientListByBillingSubscriptionOptions contains the optional parameters for the InvoicesClient.NewListByBillingSubscriptionPager
//     method.
func (client *InvoicesClient) NewListByBillingSubscriptionPager(options *InvoicesClientListByBillingSubscriptionOptions) *runtime.Pager[InvoicesClientListByBillingSubscriptionResponse] {
	return runtime.NewPager(runtime.PagingHandler[InvoicesClientListByBillingSubscriptionResponse]{
		More: func(page InvoicesClientListByBillingSubscriptionResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *InvoicesClientListByBillingSubscriptionResponse) (InvoicesClientListByBillingSubscriptionResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "InvoicesClient.NewListByBillingSubscriptionPager")
			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.listByBillingSubscriptionCreateRequest(ctx, options)
			}, nil)
			if err != nil {
				return InvoicesClientListByBillingSubscriptionResponse{}, err
			}
			return client.listByBillingSubscriptionHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByBillingSubscriptionCreateRequest creates the ListByBillingSubscription request.
func (client *InvoicesClient) listByBillingSubscriptionCreateRequest(ctx context.Context, options *InvoicesClientListByBillingSubscriptionOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Billing/billingAccounts/default/billingSubscriptions/{subscriptionId}/invoices"
	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", "2024-04-01")
	if options != nil && options.Count != nil {
		reqQP.Set("count", strconv.FormatBool(*options.Count))
	}
	if options != nil && options.Filter != nil {
		reqQP.Set("filter", *options.Filter)
	}
	if options != nil && options.OrderBy != nil {
		reqQP.Set("orderBy", *options.OrderBy)
	}
	if options != nil && options.PeriodEndDate != nil {
		reqQP.Set("periodEndDate", options.PeriodEndDate.Format("2006-01-02"))
	}
	if options != nil && options.PeriodStartDate != nil {
		reqQP.Set("periodStartDate", options.PeriodStartDate.Format("2006-01-02"))
	}
	if options != nil && options.Search != nil {
		reqQP.Set("search", *options.Search)
	}
	if options != nil && options.Skip != nil {
		reqQP.Set("skip", strconv.FormatInt(*options.Skip, 10))
	}
	if options != nil && options.Top != nil {
		reqQP.Set("top", strconv.FormatInt(*options.Top, 10))
	}
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByBillingSubscriptionHandleResponse handles the ListByBillingSubscription response.
func (client *InvoicesClient) listByBillingSubscriptionHandleResponse(resp *http.Response) (InvoicesClientListByBillingSubscriptionResponse, error) {
	result := InvoicesClientListByBillingSubscriptionResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.InvoiceListResult); err != nil {
		return InvoicesClientListByBillingSubscriptionResponse{}, err
	}
	return result, nil
}
