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

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

// DeploymentWafPoliciesClient contains the methods for the DeploymentWafPolicies group.
// Don't use this type directly, use NewDeploymentWafPoliciesClient() instead.
//
// Generated from API version 2025-11-01
type DeploymentWafPoliciesClient struct {
	internal       *arm.Client
	subscriptionID string
}

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

// Analysis - Analyze an Nginx Waf Policy
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - deploymentName - The name of targeted NGINX deployment
//   - wafPolicyName - The name of Waf Policy
//   - options - DeploymentWafPoliciesClientAnalysisOptions contains the optional parameters for the DeploymentWafPoliciesClient.Analysis
//     method.
func (client *DeploymentWafPoliciesClient) Analysis(ctx context.Context, resourceGroupName string, deploymentName string, wafPolicyName string, options *DeploymentWafPoliciesClientAnalysisOptions) (DeploymentWafPoliciesClientAnalysisResponse, error) {
	var err error
	const operationName = "DeploymentWafPoliciesClient.Analysis"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.analysisCreateRequest(ctx, resourceGroupName, deploymentName, wafPolicyName, options)
	if err != nil {
		return DeploymentWafPoliciesClientAnalysisResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return DeploymentWafPoliciesClientAnalysisResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return DeploymentWafPoliciesClientAnalysisResponse{}, err
	}
	resp, err := client.analysisHandleResponse(httpResp)
	return resp, err
}

// analysisCreateRequest creates the Analysis request.
func (client *DeploymentWafPoliciesClient) analysisCreateRequest(ctx context.Context, resourceGroupName string, deploymentName string, wafPolicyName string, options *DeploymentWafPoliciesClientAnalysisOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Nginx.NginxPlus/nginxDeployments/{deploymentName}/wafPolicies/{wafPolicyName}/analyzeWafPolicy"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if deploymentName == "" {
		return nil, errors.New("parameter deploymentName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{deploymentName}", url.PathEscape(deploymentName))
	if wafPolicyName == "" {
		return nil, errors.New("parameter wafPolicyName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{wafPolicyName}", url.PathEscape(wafPolicyName))
	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", version20251101)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	if options != nil && options.Body != nil {
		req.Raw().Header["Content-Type"] = []string{"application/json"}
		if err := runtime.MarshalAsJSON(req, *options.Body); err != nil {
			return nil, err
		}
		return req, nil
	}
	return req, nil
}

// analysisHandleResponse handles the Analysis response.
func (client *DeploymentWafPoliciesClient) analysisHandleResponse(resp *http.Response) (DeploymentWafPoliciesClientAnalysisResponse, error) {
	result := DeploymentWafPoliciesClientAnalysisResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.DeploymentWafPolicyAnalysisResponse); err != nil {
		return DeploymentWafPoliciesClientAnalysisResponse{}, err
	}
	return result, nil
}
