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

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

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

// Cancel - Cancels a transfer request. The operation is supported only 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.
//   - billingProfileName - The ID that uniquely identifies a billing profile.
//   - invoiceSectionName - The ID that uniquely identifies an invoice section.
//   - transferName - The ID that uniquely identifies a transfer request.
//   - options - TransfersClientCancelOptions contains the optional parameters for the TransfersClient.Cancel method.
func (client *TransfersClient) Cancel(ctx context.Context, billingAccountName string, billingProfileName string, invoiceSectionName string, transferName string, options *TransfersClientCancelOptions) (TransfersClientCancelResponse, error) {
	var err error
	const operationName = "TransfersClient.Cancel"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.cancelCreateRequest(ctx, billingAccountName, billingProfileName, invoiceSectionName, transferName, options)
	if err != nil {
		return TransfersClientCancelResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return TransfersClientCancelResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return TransfersClientCancelResponse{}, err
	}
	resp, err := client.cancelHandleResponse(httpResp)
	return resp, err
}

// cancelCreateRequest creates the Cancel request.
func (client *TransfersClient) cancelCreateRequest(ctx context.Context, billingAccountName string, billingProfileName string, invoiceSectionName string, transferName string, options *TransfersClientCancelOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Billing/billingAccounts/{billingAccountName}/billingProfiles/{billingProfileName}/invoiceSections/{invoiceSectionName}/transfers/{transferName}/cancel"
	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))
	if invoiceSectionName == "" {
		return nil, errors.New("parameter invoiceSectionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{invoiceSectionName}", url.PathEscape(invoiceSectionName))
	if transferName == "" {
		return nil, errors.New("parameter transferName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{transferName}", url.PathEscape(transferName))
	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
}

// cancelHandleResponse handles the Cancel response.
func (client *TransfersClient) cancelHandleResponse(resp *http.Response) (TransfersClientCancelResponse, error) {
	result := TransfersClientCancelResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.TransferDetails); err != nil {
		return TransfersClientCancelResponse{}, err
	}
	return result, nil
}

// Get - Gets a transfer request by ID. The operation is supported only 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.
//   - billingProfileName - The ID that uniquely identifies a billing profile.
//   - invoiceSectionName - The ID that uniquely identifies an invoice section.
//   - transferName - The ID that uniquely identifies a transfer request.
//   - options - TransfersClientGetOptions contains the optional parameters for the TransfersClient.Get method.
func (client *TransfersClient) Get(ctx context.Context, billingAccountName string, billingProfileName string, invoiceSectionName string, transferName string, options *TransfersClientGetOptions) (TransfersClientGetResponse, error) {
	var err error
	const operationName = "TransfersClient.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, billingAccountName, billingProfileName, invoiceSectionName, transferName, options)
	if err != nil {
		return TransfersClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return TransfersClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return TransfersClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *TransfersClient) getCreateRequest(ctx context.Context, billingAccountName string, billingProfileName string, invoiceSectionName string, transferName string, options *TransfersClientGetOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Billing/billingAccounts/{billingAccountName}/billingProfiles/{billingProfileName}/invoiceSections/{invoiceSectionName}/transfers/{transferName}"
	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))
	if invoiceSectionName == "" {
		return nil, errors.New("parameter invoiceSectionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{invoiceSectionName}", url.PathEscape(invoiceSectionName))
	if transferName == "" {
		return nil, errors.New("parameter transferName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{transferName}", url.PathEscape(transferName))
	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 *TransfersClient) getHandleResponse(resp *http.Response) (TransfersClientGetResponse, error) {
	result := TransfersClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.TransferDetails); err != nil {
		return TransfersClientGetResponse{}, err
	}
	return result, nil
}

// Initiate - Sends a request to a user in another billing account to transfer billing ownership of their subscriptions. The
// operation is supported only 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.
//   - billingProfileName - The ID that uniquely identifies a billing profile.
//   - invoiceSectionName - The ID that uniquely identifies an invoice section.
//   - transferName - The ID that uniquely identifies a transfer request.
//   - parameters - Request parameters that are provided to the initiate transfer operation.
//   - options - TransfersClientInitiateOptions contains the optional parameters for the TransfersClient.Initiate method.
func (client *TransfersClient) Initiate(ctx context.Context, billingAccountName string, billingProfileName string, invoiceSectionName string, transferName string, parameters InitiateTransferRequest, options *TransfersClientInitiateOptions) (TransfersClientInitiateResponse, error) {
	var err error
	const operationName = "TransfersClient.Initiate"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.initiateCreateRequest(ctx, billingAccountName, billingProfileName, invoiceSectionName, transferName, parameters, options)
	if err != nil {
		return TransfersClientInitiateResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return TransfersClientInitiateResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
		err = runtime.NewResponseError(httpResp)
		return TransfersClientInitiateResponse{}, err
	}
	resp, err := client.initiateHandleResponse(httpResp)
	return resp, err
}

// initiateCreateRequest creates the Initiate request.
func (client *TransfersClient) initiateCreateRequest(ctx context.Context, billingAccountName string, billingProfileName string, invoiceSectionName string, transferName string, parameters InitiateTransferRequest, options *TransfersClientInitiateOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Billing/billingAccounts/{billingAccountName}/billingProfiles/{billingProfileName}/invoiceSections/{invoiceSectionName}/transfers/{transferName}"
	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))
	if invoiceSectionName == "" {
		return nil, errors.New("parameter invoiceSectionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{invoiceSectionName}", url.PathEscape(invoiceSectionName))
	if transferName == "" {
		return nil, errors.New("parameter transferName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{transferName}", url.PathEscape(transferName))
	req, err := runtime.NewRequest(ctx, http.MethodPut, 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
}

// initiateHandleResponse handles the Initiate response.
func (client *TransfersClient) initiateHandleResponse(resp *http.Response) (TransfersClientInitiateResponse, error) {
	result := TransfersClientInitiateResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.TransferDetails); err != nil {
		return TransfersClientInitiateResponse{}, err
	}
	return result, nil
}

// NewListPager - Lists the transfer requests for an invoice section. The operation is supported only for billing accounts
// with agreement type 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.
//   - invoiceSectionName - The ID that uniquely identifies an invoice section.
//   - options - TransfersClientListOptions contains the optional parameters for the TransfersClient.NewListPager method.
func (client *TransfersClient) NewListPager(billingAccountName string, billingProfileName string, invoiceSectionName string, options *TransfersClientListOptions) *runtime.Pager[TransfersClientListResponse] {
	return runtime.NewPager(runtime.PagingHandler[TransfersClientListResponse]{
		More: func(page TransfersClientListResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *TransfersClientListResponse) (TransfersClientListResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "TransfersClient.NewListPager")
			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.listCreateRequest(ctx, billingAccountName, billingProfileName, invoiceSectionName, options)
			}, nil)
			if err != nil {
				return TransfersClientListResponse{}, err
			}
			return client.listHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listCreateRequest creates the List request.
func (client *TransfersClient) listCreateRequest(ctx context.Context, billingAccountName string, billingProfileName string, invoiceSectionName string, options *TransfersClientListOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Billing/billingAccounts/{billingAccountName}/billingProfiles/{billingProfileName}/invoiceSections/{invoiceSectionName}/transfers"
	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))
	if invoiceSectionName == "" {
		return nil, errors.New("parameter invoiceSectionName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{invoiceSectionName}", url.PathEscape(invoiceSectionName))
	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
}

// listHandleResponse handles the List response.
func (client *TransfersClient) listHandleResponse(resp *http.Response) (TransfersClientListResponse, error) {
	result := TransfersClientListResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.TransferDetailsListResult); err != nil {
		return TransfersClientListResponse{}, err
	}
	return result, nil
}
