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

import (
	"context"
	"errors"
	"fmt"
	azfake "github.com/Azure/azure-sdk-for-go/sdk/azcore/fake"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/fake/server"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/nginx/armnginx/v4"
	"net/http"
	"net/url"
	"reflect"
	"regexp"
	"slices"
)

// DeploymentWafPoliciesServer is a fake server for instances of the armnginx.DeploymentWafPoliciesClient type.
type DeploymentWafPoliciesServer struct {
	// Analysis is the fake for method DeploymentWafPoliciesClient.Analysis
	// HTTP status codes to indicate success: http.StatusOK
	Analysis func(ctx context.Context, resourceGroupName string, deploymentName string, wafPolicyName string, options *armnginx.DeploymentWafPoliciesClientAnalysisOptions) (resp azfake.Responder[armnginx.DeploymentWafPoliciesClientAnalysisResponse], errResp azfake.ErrorResponder)
}

// NewDeploymentWafPoliciesServerTransport creates a new instance of DeploymentWafPoliciesServerTransport with the provided implementation.
// The returned DeploymentWafPoliciesServerTransport instance is connected to an instance of armnginx.DeploymentWafPoliciesClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewDeploymentWafPoliciesServerTransport(srv *DeploymentWafPoliciesServer) *DeploymentWafPoliciesServerTransport {
	return &DeploymentWafPoliciesServerTransport{srv: srv}
}

// DeploymentWafPoliciesServerTransport connects instances of armnginx.DeploymentWafPoliciesClient to instances of DeploymentWafPoliciesServer.
// Don't use this type directly, use NewDeploymentWafPoliciesServerTransport instead.
type DeploymentWafPoliciesServerTransport struct {
	srv *DeploymentWafPoliciesServer
}

// Do implements the policy.Transporter interface for DeploymentWafPoliciesServerTransport.
func (d *DeploymentWafPoliciesServerTransport) Do(req *http.Request) (*http.Response, error) {
	rawMethod := req.Context().Value(runtime.CtxAPINameKey{})
	method, ok := rawMethod.(string)
	if !ok {
		return nil, nonRetriableError{errors.New("unable to dispatch request, missing value for CtxAPINameKey")}
	}

	return d.dispatchToMethodFake(req, method)
}

func (d *DeploymentWafPoliciesServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if deploymentWafPoliciesServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = deploymentWafPoliciesServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "DeploymentWafPoliciesClient.Analysis":
				res.resp, res.err = d.dispatchAnalysis(req)
			default:
				res.err = fmt.Errorf("unhandled API %s", method)
			}

		}
		resultChan <- res
	}()

	select {
	case <-req.Context().Done():
		return nil, req.Context().Err()
	case res := <-resultChan:
		return res.resp, res.err
	}
}

func (d *DeploymentWafPoliciesServerTransport) dispatchAnalysis(req *http.Request) (*http.Response, error) {
	if d.srv.Analysis == nil {
		return nil, &nonRetriableError{errors.New("fake for method Analysis not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Nginx\.NginxPlus/nginxDeployments/(?P<deploymentName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/wafPolicies/(?P<wafPolicyName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/analyzeWafPolicy`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 5 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	body, err := server.UnmarshalRequestAsJSON[armnginx.DeploymentWafPolicyAnalysisCreateRequest](req)
	if err != nil {
		return nil, err
	}
	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
	if err != nil {
		return nil, err
	}
	deploymentNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("deploymentName")])
	if err != nil {
		return nil, err
	}
	wafPolicyNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("wafPolicyName")])
	if err != nil {
		return nil, err
	}
	var options *armnginx.DeploymentWafPoliciesClientAnalysisOptions
	if !reflect.ValueOf(body).IsZero() {
		options = &armnginx.DeploymentWafPoliciesClientAnalysisOptions{
			Body: &body,
		}
	}
	respr, errRespr := d.srv.Analysis(req.Context(), resourceGroupNameParam, deploymentNameParam, wafPolicyNameParam, options)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !slices.Contains([]int{http.StatusOK}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", respContent.HTTPStatus)}
	}
	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).DeploymentWafPolicyAnalysisResponse, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

// set this to conditionally intercept incoming requests to DeploymentWafPoliciesServerTransport
var deploymentWafPoliciesServerTransportInterceptor interface {
	// Do returns true if the server transport should use the returned response/error
	Do(*http.Request) (*http.Response, error, bool)
}
