// 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/newrelic/armnewrelicobservability/v2"
	"net/http"
	"regexp"
	"slices"
)

// SaaSServer is a fake server for instances of the armnewrelicobservability.SaaSClient type.
type SaaSServer struct {
	// ActivateResource is the fake for method SaaSClient.ActivateResource
	// HTTP status codes to indicate success: http.StatusOK
	ActivateResource func(ctx context.Context, request armnewrelicobservability.ActivateSaaSParameterRequest, options *armnewrelicobservability.SaaSClientActivateResourceOptions) (resp azfake.Responder[armnewrelicobservability.SaaSClientActivateResourceResponse], errResp azfake.ErrorResponder)
}

// NewSaaSServerTransport creates a new instance of SaaSServerTransport with the provided implementation.
// The returned SaaSServerTransport instance is connected to an instance of armnewrelicobservability.SaaSClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewSaaSServerTransport(srv *SaaSServer) *SaaSServerTransport {
	return &SaaSServerTransport{srv: srv}
}

// SaaSServerTransport connects instances of armnewrelicobservability.SaaSClient to instances of SaaSServer.
// Don't use this type directly, use NewSaaSServerTransport instead.
type SaaSServerTransport struct {
	srv *SaaSServer
}

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

func (s *SaaSServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if saaSServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = saaSServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "SaaSClient.ActivateResource":
				res.resp, res.err = s.dispatchActivateResource(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 (s *SaaSServerTransport) dispatchActivateResource(req *http.Request) (*http.Response, error) {
	if s.srv.ActivateResource == nil {
		return nil, &nonRetriableError{errors.New("fake for method ActivateResource not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/NewRelic\.Observability/activateSaaS`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 2 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	body, err := server.UnmarshalRequestAsJSON[armnewrelicobservability.ActivateSaaSParameterRequest](req)
	if err != nil {
		return nil, err
	}
	respr, errRespr := s.srv.ActivateResource(req.Context(), body, 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).SaaSResourceDetailsResponse, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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