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

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

// TokensClient contains the methods for the Tokens group.
// Don't use this type directly, use NewTokensClient() instead.
//
// Generated from API version 2026-01-01-preview
type TokensClient struct {
	internal       *arm.Client
	subscriptionID string
}

// NewTokensClient creates a new instance of TokensClient 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 - Contains optional client configuration. Pass nil to accept the default values.
func NewTokensClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*TokensClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &TokensClient{
		subscriptionID: subscriptionID,
		internal:       cl,
	}
	return client, nil
}

// Acquire - Acquires a policy token.
//
// This operation acquires a policy token in the given subscription for the given request body.
// If the operation fails it returns an *azcore.ResponseError type.
//   - parameters - The request body
//   - options - TokensClientAcquireOptions contains the optional parameters for the TokensClient.Acquire method.
func (client *TokensClient) Acquire(ctx context.Context, parameters TokenRequest, options *TokensClientAcquireOptions) (TokensClientAcquireResponse, error) {
	var err error
	const operationName = "TokensClient.Acquire"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.acquireCreateRequest(ctx, parameters, options)
	if err != nil {
		return TokensClientAcquireResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return TokensClientAcquireResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return TokensClientAcquireResponse{}, err
	}
	resp, err := client.acquireHandleResponse(httpResp)
	return resp, err
}

// acquireCreateRequest creates the Acquire request.
func (client *TokensClient) acquireCreateRequest(ctx context.Context, parameters TokenRequest, _ *TokensClientAcquireOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.Authorization/acquirePolicyToken"
	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", version20260101Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	req.Raw().Header["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, parameters); err != nil {
		return nil, err
	}
	return req, nil
}

// acquireHandleResponse handles the Acquire response.
func (client *TokensClient) acquireHandleResponse(resp *http.Response) (TokensClientAcquireResponse, error) {
	result := TokensClientAcquireResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.TokenResponse); err != nil {
		return TokensClientAcquireResponse{}, err
	}
	return result, nil
}

// AcquireAtManagementGroup - Acquires a policy token at management group level.
//
// This operation acquires a policy token in the given management group for the given request body.
// If the operation fails it returns an *azcore.ResponseError type.
//   - managementGroupName - The name of the management group. The name is case insensitive.
//   - parameters - The policy token properties.
//   - options - TokensClientAcquireAtManagementGroupOptions contains the optional parameters for the TokensClient.AcquireAtManagementGroup
//     method.
func (client *TokensClient) AcquireAtManagementGroup(ctx context.Context, managementGroupName string, parameters TokenRequest, options *TokensClientAcquireAtManagementGroupOptions) (TokensClientAcquireAtManagementGroupResponse, error) {
	var err error
	const operationName = "TokensClient.AcquireAtManagementGroup"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.acquireAtManagementGroupCreateRequest(ctx, managementGroupName, parameters, options)
	if err != nil {
		return TokensClientAcquireAtManagementGroupResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return TokensClientAcquireAtManagementGroupResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return TokensClientAcquireAtManagementGroupResponse{}, err
	}
	resp, err := client.acquireAtManagementGroupHandleResponse(httpResp)
	return resp, err
}

// acquireAtManagementGroupCreateRequest creates the AcquireAtManagementGroup request.
func (client *TokensClient) acquireAtManagementGroupCreateRequest(ctx context.Context, managementGroupName string, parameters TokenRequest, _ *TokensClientAcquireAtManagementGroupOptions) (*policy.Request, error) {
	urlPath := "/providers/Microsoft.Management/managementGroups/{managementGroupName}/providers/Microsoft.Authorization/acquirePolicyToken"
	if managementGroupName == "" {
		return nil, errors.New("parameter managementGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{managementGroupName}", url.PathEscape(managementGroupName))
	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", version20260101Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	req.Raw().Header["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, parameters); err != nil {
		return nil, err
	}
	return req, nil
}

// acquireAtManagementGroupHandleResponse handles the AcquireAtManagementGroup response.
func (client *TokensClient) acquireAtManagementGroupHandleResponse(resp *http.Response) (TokensClientAcquireAtManagementGroupResponse, error) {
	result := TokensClientAcquireAtManagementGroupResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.TokenResponse); err != nil {
		return TokensClientAcquireAtManagementGroupResponse{}, err
	}
	return result, nil
}
