// 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/costmanagement/armcostmanagement/v3"
	"net/http"
	"net/url"
	"regexp"
	"slices"
)

// GenerateDetailedCostReportServer is a fake server for instances of the armcostmanagement.GenerateDetailedCostReportClient type.
type GenerateDetailedCostReportServer struct {
	// BeginCreateOperation is the fake for method GenerateDetailedCostReportClient.BeginCreateOperation
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginCreateOperation func(ctx context.Context, scope string, parameters armcostmanagement.GenerateDetailedCostReportDefinition, options *armcostmanagement.GenerateDetailedCostReportClientBeginCreateOperationOptions) (resp azfake.PollerResponder[armcostmanagement.GenerateDetailedCostReportClientCreateOperationResponse], errResp azfake.ErrorResponder)
}

// NewGenerateDetailedCostReportServerTransport creates a new instance of GenerateDetailedCostReportServerTransport with the provided implementation.
// The returned GenerateDetailedCostReportServerTransport instance is connected to an instance of armcostmanagement.GenerateDetailedCostReportClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewGenerateDetailedCostReportServerTransport(srv *GenerateDetailedCostReportServer) *GenerateDetailedCostReportServerTransport {
	return &GenerateDetailedCostReportServerTransport{
		srv:                  srv,
		beginCreateOperation: newTracker[azfake.PollerResponder[armcostmanagement.GenerateDetailedCostReportClientCreateOperationResponse]](),
	}
}

// GenerateDetailedCostReportServerTransport connects instances of armcostmanagement.GenerateDetailedCostReportClient to instances of GenerateDetailedCostReportServer.
// Don't use this type directly, use NewGenerateDetailedCostReportServerTransport instead.
type GenerateDetailedCostReportServerTransport struct {
	srv                  *GenerateDetailedCostReportServer
	beginCreateOperation *tracker[azfake.PollerResponder[armcostmanagement.GenerateDetailedCostReportClientCreateOperationResponse]]
}

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

func (g *GenerateDetailedCostReportServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if generateDetailedCostReportServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = generateDetailedCostReportServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "GenerateDetailedCostReportClient.BeginCreateOperation":
				res.resp, res.err = g.dispatchBeginCreateOperation(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 (g *GenerateDetailedCostReportServerTransport) dispatchBeginCreateOperation(req *http.Request) (*http.Response, error) {
	if g.srv.BeginCreateOperation == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCreateOperation not implemented")}
	}
	beginCreateOperation := g.beginCreateOperation.get(req)
	if beginCreateOperation == nil {
		const regexStr = `/(?P<scope>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.CostManagement/generateDetailedCostReport`
		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[armcostmanagement.GenerateDetailedCostReportDefinition](req)
		if err != nil {
			return nil, err
		}
		scopeParam, err := url.PathUnescape(matches[regex.SubexpIndex("scope")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := g.srv.BeginCreateOperation(req.Context(), scopeParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCreateOperation = &respr
		g.beginCreateOperation.add(req, beginCreateOperation)
	}

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

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

	return resp, nil
}

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