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

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

	// BeginDelete is the fake for method RaiExternalSafetyProviderClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, safetyProviderName string, options *armcognitiveservices.RaiExternalSafetyProviderClientBeginDeleteOptions) (resp azfake.PollerResponder[armcognitiveservices.RaiExternalSafetyProviderClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method RaiExternalSafetyProviderClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, safetyProviderName string, options *armcognitiveservices.RaiExternalSafetyProviderClientGetOptions) (resp azfake.Responder[armcognitiveservices.RaiExternalSafetyProviderClientGetResponse], errResp azfake.ErrorResponder)
}

// NewRaiExternalSafetyProviderServerTransport creates a new instance of RaiExternalSafetyProviderServerTransport with the provided implementation.
// The returned RaiExternalSafetyProviderServerTransport instance is connected to an instance of armcognitiveservices.RaiExternalSafetyProviderClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewRaiExternalSafetyProviderServerTransport(srv *RaiExternalSafetyProviderServer) *RaiExternalSafetyProviderServerTransport {
	return &RaiExternalSafetyProviderServerTransport{
		srv:         srv,
		beginDelete: newTracker[azfake.PollerResponder[armcognitiveservices.RaiExternalSafetyProviderClientDeleteResponse]](),
	}
}

// RaiExternalSafetyProviderServerTransport connects instances of armcognitiveservices.RaiExternalSafetyProviderClient to instances of RaiExternalSafetyProviderServer.
// Don't use this type directly, use NewRaiExternalSafetyProviderServerTransport instead.
type RaiExternalSafetyProviderServerTransport struct {
	srv         *RaiExternalSafetyProviderServer
	beginDelete *tracker[azfake.PollerResponder[armcognitiveservices.RaiExternalSafetyProviderClientDeleteResponse]]
}

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

func (r *RaiExternalSafetyProviderServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if raiExternalSafetyProviderServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = raiExternalSafetyProviderServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "RaiExternalSafetyProviderClient.CreateOrUpdate":
				res.resp, res.err = r.dispatchCreateOrUpdate(req)
			case "RaiExternalSafetyProviderClient.BeginDelete":
				res.resp, res.err = r.dispatchBeginDelete(req)
			case "RaiExternalSafetyProviderClient.Get":
				res.resp, res.err = r.dispatchGet(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 (r *RaiExternalSafetyProviderServerTransport) dispatchCreateOrUpdate(req *http.Request) (*http.Response, error) {
	if r.srv.CreateOrUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method CreateOrUpdate not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.CognitiveServices/raiExternalSafetyProviders/(?P<safetyProviderName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 3 {
		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
	}
	safetyProviderNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("safetyProviderName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := r.srv.CreateOrUpdate(req.Context(), 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
}

func (r *RaiExternalSafetyProviderServerTransport) dispatchBeginDelete(req *http.Request) (*http.Response, error) {
	if r.srv.BeginDelete == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginDelete not implemented")}
	}
	beginDelete := r.beginDelete.get(req)
	if beginDelete == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.CognitiveServices/raiExternalSafetyProviders/(?P<safetyProviderName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 3 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		safetyProviderNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("safetyProviderName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := r.srv.BeginDelete(req.Context(), safetyProviderNameParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginDelete = &respr
		r.beginDelete.add(req, beginDelete)
	}

	resp, err := server.PollerResponderNext(beginDelete, req)
	if err != nil {
		return nil, err
	}

	if !slices.Contains([]int{http.StatusOK, http.StatusAccepted, http.StatusNoContent}, resp.StatusCode) {
		r.beginDelete.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusAccepted, http.StatusNoContent", resp.StatusCode)}
	}
	if !server.PollerResponderMore(beginDelete) {
		r.beginDelete.remove(req)
	}

	return resp, nil
}

func (r *RaiExternalSafetyProviderServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if r.srv.Get == nil {
		return nil, &nonRetriableError{errors.New("fake for method Get not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.CognitiveServices/raiExternalSafetyProviders/(?P<safetyProviderName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 3 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	safetyProviderNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("safetyProviderName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := r.srv.Get(req.Context(), safetyProviderNameParam, nil)
	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).RaiExternalSafetyProviderSchema, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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