// 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/cognitiveservices/armcognitiveservices/v4"
	"net/http"
	"net/url"
	"regexp"
	"slices"
)

// TestRaiExternalSafetyProviderServer is a fake server for instances of the armcognitiveservices.TestRaiExternalSafetyProviderClient type.
type TestRaiExternalSafetyProviderServer struct {
	// CreateOrUpdate is the fake for method TestRaiExternalSafetyProviderClient.CreateOrUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	CreateOrUpdate func(ctx context.Context, resourceGroupName string, accountName string, safetyProviderName string, safetyProvider armcognitiveservices.RaiExternalSafetyProviderSchema, options *armcognitiveservices.TestRaiExternalSafetyProviderClientCreateOrUpdateOptions) (resp azfake.Responder[armcognitiveservices.TestRaiExternalSafetyProviderClientCreateOrUpdateResponse], errResp azfake.ErrorResponder)
}

// NewTestRaiExternalSafetyProviderServerTransport creates a new instance of TestRaiExternalSafetyProviderServerTransport with the provided implementation.
// The returned TestRaiExternalSafetyProviderServerTransport instance is connected to an instance of armcognitiveservices.TestRaiExternalSafetyProviderClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewTestRaiExternalSafetyProviderServerTransport(srv *TestRaiExternalSafetyProviderServer) *TestRaiExternalSafetyProviderServerTransport {
	return &TestRaiExternalSafetyProviderServerTransport{srv: srv}
}

// TestRaiExternalSafetyProviderServerTransport connects instances of armcognitiveservices.TestRaiExternalSafetyProviderClient to instances of TestRaiExternalSafetyProviderServer.
// Don't use this type directly, use NewTestRaiExternalSafetyProviderServerTransport instead.
type TestRaiExternalSafetyProviderServerTransport struct {
	srv *TestRaiExternalSafetyProviderServer
}

// Do implements the policy.Transporter interface for TestRaiExternalSafetyProviderServerTransport.
func (t *TestRaiExternalSafetyProviderServerTransport) 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 t.dispatchToMethodFake(req, method)
}

func (t *TestRaiExternalSafetyProviderServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if testRaiExternalSafetyProviderServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = testRaiExternalSafetyProviderServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "TestRaiExternalSafetyProviderClient.CreateOrUpdate":
				res.resp, res.err = t.dispatchCreateOrUpdate(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 (t *TestRaiExternalSafetyProviderServerTransport) dispatchCreateOrUpdate(req *http.Request) (*http.Response, error) {
	if t.srv.CreateOrUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method CreateOrUpdate not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.CognitiveServices/accounts/(?P<accountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/testRaiExternalSafetyProvider/(?P<safetyProviderName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	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[armcognitiveservices.RaiExternalSafetyProviderSchema](req)
	if err != nil {
		return nil, err
	}
	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
	if err != nil {
		return nil, err
	}
	accountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("accountName")])
	if err != nil {
		return nil, err
	}
	safetyProviderNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("safetyProviderName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := t.srv.CreateOrUpdate(req.Context(), resourceGroupNameParam, accountNameParam, safetyProviderNameParam, body, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !slices.Contains([]int{http.StatusOK, http.StatusCreated}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusCreated", respContent.HTTPStatus)}
	}
	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).RaiExternalSafetyProviderSchema, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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