// 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/azcore/to"
	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/advisor/armadvisor/v2"
	"net/http"
	"net/url"
	"regexp"
	"slices"
	"strconv"
)

// TriageRecommendationsServer is a fake server for instances of the armadvisor.TriageRecommendationsClient type.
type TriageRecommendationsServer struct {
	// ApproveTriageRecommendation is the fake for method TriageRecommendationsClient.ApproveTriageRecommendation
	// HTTP status codes to indicate success: http.StatusOK
	ApproveTriageRecommendation func(ctx context.Context, reviewID string, recommendationID string, options *armadvisor.TriageRecommendationsClientApproveTriageRecommendationOptions) (resp azfake.Responder[armadvisor.TriageRecommendationsClientApproveTriageRecommendationResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method TriageRecommendationsClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, reviewID string, recommendationID string, options *armadvisor.TriageRecommendationsClientGetOptions) (resp azfake.Responder[armadvisor.TriageRecommendationsClientGetResponse], errResp azfake.ErrorResponder)

	// NewListPager is the fake for method TriageRecommendationsClient.NewListPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListPager func(reviewID string, options *armadvisor.TriageRecommendationsClientListOptions) (resp azfake.PagerResponder[armadvisor.TriageRecommendationsClientListResponse])

	// RejectTriageRecommendation is the fake for method TriageRecommendationsClient.RejectTriageRecommendation
	// HTTP status codes to indicate success: http.StatusOK
	RejectTriageRecommendation func(ctx context.Context, reviewID string, recommendationID string, recommendationRejectBody armadvisor.RecommendationRejectBody, options *armadvisor.TriageRecommendationsClientRejectTriageRecommendationOptions) (resp azfake.Responder[armadvisor.TriageRecommendationsClientRejectTriageRecommendationResponse], errResp azfake.ErrorResponder)

	// ResetTriageRecommendation is the fake for method TriageRecommendationsClient.ResetTriageRecommendation
	// HTTP status codes to indicate success: http.StatusOK
	ResetTriageRecommendation func(ctx context.Context, reviewID string, recommendationID string, options *armadvisor.TriageRecommendationsClientResetTriageRecommendationOptions) (resp azfake.Responder[armadvisor.TriageRecommendationsClientResetTriageRecommendationResponse], errResp azfake.ErrorResponder)
}

// NewTriageRecommendationsServerTransport creates a new instance of TriageRecommendationsServerTransport with the provided implementation.
// The returned TriageRecommendationsServerTransport instance is connected to an instance of armadvisor.TriageRecommendationsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewTriageRecommendationsServerTransport(srv *TriageRecommendationsServer) *TriageRecommendationsServerTransport {
	return &TriageRecommendationsServerTransport{
		srv:          srv,
		newListPager: newTracker[azfake.PagerResponder[armadvisor.TriageRecommendationsClientListResponse]](),
	}
}

// TriageRecommendationsServerTransport connects instances of armadvisor.TriageRecommendationsClient to instances of TriageRecommendationsServer.
// Don't use this type directly, use NewTriageRecommendationsServerTransport instead.
type TriageRecommendationsServerTransport struct {
	srv          *TriageRecommendationsServer
	newListPager *tracker[azfake.PagerResponder[armadvisor.TriageRecommendationsClientListResponse]]
}

// Do implements the policy.Transporter interface for TriageRecommendationsServerTransport.
func (t *TriageRecommendationsServerTransport) 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 *TriageRecommendationsServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if triageRecommendationsServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = triageRecommendationsServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "TriageRecommendationsClient.ApproveTriageRecommendation":
				res.resp, res.err = t.dispatchApproveTriageRecommendation(req)
			case "TriageRecommendationsClient.Get":
				res.resp, res.err = t.dispatchGet(req)
			case "TriageRecommendationsClient.NewListPager":
				res.resp, res.err = t.dispatchNewListPager(req)
			case "TriageRecommendationsClient.RejectTriageRecommendation":
				res.resp, res.err = t.dispatchRejectTriageRecommendation(req)
			case "TriageRecommendationsClient.ResetTriageRecommendation":
				res.resp, res.err = t.dispatchResetTriageRecommendation(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 *TriageRecommendationsServerTransport) dispatchApproveTriageRecommendation(req *http.Request) (*http.Response, error) {
	if t.srv.ApproveTriageRecommendation == nil {
		return nil, &nonRetriableError{errors.New("fake for method ApproveTriageRecommendation not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Advisor/resiliencyReviews/(?P<reviewId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Advisor/triageRecommendations/(?P<recommendationId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/approve`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 4 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	reviewIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("reviewId")])
	if err != nil {
		return nil, err
	}
	recommendationIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("recommendationId")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := t.srv.ApproveTriageRecommendation(req.Context(), reviewIDParam, recommendationIDParam, 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.NewResponse(respContent, req, nil)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (t *TriageRecommendationsServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if t.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\.Advisor/resiliencyReviews/(?P<reviewId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Advisor/triageRecommendations/(?P<recommendationId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 4 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	reviewIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("reviewId")])
	if err != nil {
		return nil, err
	}
	recommendationIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("recommendationId")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := t.srv.Get(req.Context(), reviewIDParam, recommendationIDParam, 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).TriageRecommendation, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (t *TriageRecommendationsServerTransport) dispatchNewListPager(req *http.Request) (*http.Response, error) {
	if t.srv.NewListPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListPager not implemented")}
	}
	newListPager := t.newListPager.get(req)
	if newListPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Advisor/resiliencyReviews/(?P<reviewId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Advisor/triageRecommendations`
		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)
		}
		qp := req.URL.Query()
		reviewIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("reviewId")])
		if err != nil {
			return nil, err
		}
		topParam, err := parseOptional(qp.Get("$top"), func(v string) (int32, error) {
			p, parseErr := strconv.ParseInt(v, 10, 32)
			if parseErr != nil {
				return 0, parseErr
			}
			return int32(p), nil
		})
		if err != nil {
			return nil, err
		}
		skipParam, err := parseOptional(qp.Get("$skip"), func(v string) (int32, error) {
			p, parseErr := strconv.ParseInt(v, 10, 32)
			if parseErr != nil {
				return 0, parseErr
			}
			return int32(p), nil
		})
		if err != nil {
			return nil, err
		}
		var options *armadvisor.TriageRecommendationsClientListOptions
		if topParam != nil || skipParam != nil {
			options = &armadvisor.TriageRecommendationsClientListOptions{
				Top:  topParam,
				Skip: skipParam,
			}
		}
		resp := t.srv.NewListPager(reviewIDParam, options)
		newListPager = &resp
		t.newListPager.add(req, newListPager)
		server.PagerResponderInjectNextLinks(newListPager, req, func(page *armadvisor.TriageRecommendationsClientListResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListPager, req)
	if err != nil {
		return nil, err
	}
	if !slices.Contains([]int{http.StatusOK}, resp.StatusCode) {
		t.newListPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListPager) {
		t.newListPager.remove(req)
	}
	return resp, nil
}

func (t *TriageRecommendationsServerTransport) dispatchRejectTriageRecommendation(req *http.Request) (*http.Response, error) {
	if t.srv.RejectTriageRecommendation == nil {
		return nil, &nonRetriableError{errors.New("fake for method RejectTriageRecommendation not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Advisor/resiliencyReviews/(?P<reviewId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Advisor/triageRecommendations/(?P<recommendationId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/reject`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 4 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	body, err := server.UnmarshalRequestAsJSON[armadvisor.RecommendationRejectBody](req)
	if err != nil {
		return nil, err
	}
	reviewIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("reviewId")])
	if err != nil {
		return nil, err
	}
	recommendationIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("recommendationId")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := t.srv.RejectTriageRecommendation(req.Context(), reviewIDParam, recommendationIDParam, 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.NewResponse(respContent, req, nil)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (t *TriageRecommendationsServerTransport) dispatchResetTriageRecommendation(req *http.Request) (*http.Response, error) {
	if t.srv.ResetTriageRecommendation == nil {
		return nil, &nonRetriableError{errors.New("fake for method ResetTriageRecommendation not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Advisor/resiliencyReviews/(?P<reviewId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Advisor/triageRecommendations/(?P<recommendationId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/reset`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 4 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	reviewIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("reviewId")])
	if err != nil {
		return nil, err
	}
	recommendationIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("recommendationId")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := t.srv.ResetTriageRecommendation(req.Context(), reviewIDParam, recommendationIDParam, 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.NewResponse(respContent, req, nil)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

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