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

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

// SubscriptionRaiPolicyClient contains the methods for the SubscriptionRaiPolicy group.
// Don't use this type directly, use NewSubscriptionRaiPolicyClient() instead.
//
// Generated from API version 2026-05-15-preview
type SubscriptionRaiPolicyClient struct {
	internal       *arm.Client
	subscriptionID string
}

// NewSubscriptionRaiPolicyClient creates a new instance of SubscriptionRaiPolicyClient 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 NewSubscriptionRaiPolicyClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*SubscriptionRaiPolicyClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &SubscriptionRaiPolicyClient{
		subscriptionID: subscriptionID,
		internal:       cl,
	}
	return client, nil
}

// CreateOrUpdate - Update the state of specified Content Filters associated with the subscription.
// If the operation fails it returns an *azcore.ResponseError type.
//   - raiPolicyName - The name of the RaiPolicy associated with the Cognitive Services Account
//   - raiPolicy - Properties describing the Content Filters.
//   - options - SubscriptionRaiPolicyClientCreateOrUpdateOptions contains the optional parameters for the SubscriptionRaiPolicyClient.CreateOrUpdate
//     method.
func (client *SubscriptionRaiPolicyClient) CreateOrUpdate(ctx context.Context, raiPolicyName string, raiPolicy RaiPolicy, options *SubscriptionRaiPolicyClientCreateOrUpdateOptions) (SubscriptionRaiPolicyClientCreateOrUpdateResponse, error) {
	var err error
	const operationName = "SubscriptionRaiPolicyClient.CreateOrUpdate"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.createOrUpdateCreateRequest(ctx, raiPolicyName, raiPolicy, options)
	if err != nil {
		return SubscriptionRaiPolicyClientCreateOrUpdateResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return SubscriptionRaiPolicyClientCreateOrUpdateResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
		err = runtime.NewResponseError(httpResp)
		return SubscriptionRaiPolicyClientCreateOrUpdateResponse{}, err
	}
	resp, err := client.createOrUpdateHandleResponse(httpResp)
	return resp, err
}

// createOrUpdateCreateRequest creates the CreateOrUpdate request.
func (client *SubscriptionRaiPolicyClient) createOrUpdateCreateRequest(ctx context.Context, raiPolicyName string, raiPolicy RaiPolicy, _ *SubscriptionRaiPolicyClientCreateOrUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.CognitiveServices/raiPolicy/{raiPolicyName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if raiPolicyName == "" {
		return nil, errors.New("parameter raiPolicyName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{raiPolicyName}", url.PathEscape(raiPolicyName))
	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", version20260515Preview)
	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, raiPolicy); err != nil {
		return nil, err
	}
	return req, nil
}

// createOrUpdateHandleResponse handles the CreateOrUpdate response.
func (client *SubscriptionRaiPolicyClient) createOrUpdateHandleResponse(resp *http.Response) (SubscriptionRaiPolicyClientCreateOrUpdateResponse, error) {
	result := SubscriptionRaiPolicyClientCreateOrUpdateResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.RaiPolicy); err != nil {
		return SubscriptionRaiPolicyClientCreateOrUpdateResponse{}, err
	}
	return result, nil
}

// BeginDelete - Deletes the specified Content Filters associated with the subscription.
// If the operation fails it returns an *azcore.ResponseError type.
//   - raiPolicyName - The name of the RaiPolicy associated with the Cognitive Services Account
//   - options - SubscriptionRaiPolicyClientBeginDeleteOptions contains the optional parameters for the SubscriptionRaiPolicyClient.BeginDelete
//     method.
func (client *SubscriptionRaiPolicyClient) BeginDelete(ctx context.Context, raiPolicyName string, options *SubscriptionRaiPolicyClientBeginDeleteOptions) (*runtime.Poller[SubscriptionRaiPolicyClientDeleteResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.deleteOperation(ctx, raiPolicyName, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[SubscriptionRaiPolicyClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[SubscriptionRaiPolicyClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Delete - Deletes the specified Content Filters associated with the subscription.
// If the operation fails it returns an *azcore.ResponseError type.
func (client *SubscriptionRaiPolicyClient) deleteOperation(ctx context.Context, raiPolicyName string, options *SubscriptionRaiPolicyClientBeginDeleteOptions) (*http.Response, error) {
	var err error
	const operationName = "SubscriptionRaiPolicyClient.BeginDelete"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.deleteCreateRequest(ctx, raiPolicyName, 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, http.StatusNoContent) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// deleteCreateRequest creates the Delete request.
func (client *SubscriptionRaiPolicyClient) deleteCreateRequest(ctx context.Context, raiPolicyName string, _ *SubscriptionRaiPolicyClientBeginDeleteOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.CognitiveServices/raiPolicy/{raiPolicyName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if raiPolicyName == "" {
		return nil, errors.New("parameter raiPolicyName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{raiPolicyName}", url.PathEscape(raiPolicyName))
	req, err := runtime.NewRequest(ctx, http.MethodDelete, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", version20260515Preview)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	return req, nil
}

// Get - Gets the specified Content Filters associated with the Subscription.
// If the operation fails it returns an *azcore.ResponseError type.
//   - raiPolicyName - The name of the RaiPolicy associated with the Cognitive Services Account
//   - options - SubscriptionRaiPolicyClientGetOptions contains the optional parameters for the SubscriptionRaiPolicyClient.Get
//     method.
func (client *SubscriptionRaiPolicyClient) Get(ctx context.Context, raiPolicyName string, options *SubscriptionRaiPolicyClientGetOptions) (SubscriptionRaiPolicyClientGetResponse, error) {
	var err error
	const operationName = "SubscriptionRaiPolicyClient.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, raiPolicyName, options)
	if err != nil {
		return SubscriptionRaiPolicyClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return SubscriptionRaiPolicyClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return SubscriptionRaiPolicyClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *SubscriptionRaiPolicyClient) getCreateRequest(ctx context.Context, raiPolicyName string, _ *SubscriptionRaiPolicyClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/providers/Microsoft.CognitiveServices/raiPolicy/{raiPolicyName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if raiPolicyName == "" {
		return nil, errors.New("parameter raiPolicyName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{raiPolicyName}", url.PathEscape(raiPolicyName))
	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", version20260515Preview)
	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 *SubscriptionRaiPolicyClient) getHandleResponse(resp *http.Response) (SubscriptionRaiPolicyClientGetResponse, error) {
	result := SubscriptionRaiPolicyClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.RaiPolicy); err != nil {
		return SubscriptionRaiPolicyClientGetResponse{}, err
	}
	return result, nil
}
