// 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/securityinsights/armsecurityinsights/v2"
	"net/http"
	"net/url"
	"reflect"
	"regexp"
	"slices"
)

// EntitiesServer is a fake server for instances of the armsecurityinsights.EntitiesClient type.
type EntitiesServer struct {
	// Expand is the fake for method EntitiesClient.Expand
	// HTTP status codes to indicate success: http.StatusOK
	Expand func(ctx context.Context, resourceGroupName string, workspaceName string, entityID string, parameters armsecurityinsights.EntityExpandParameters, options *armsecurityinsights.EntitiesClientExpandOptions) (resp azfake.Responder[armsecurityinsights.EntitiesClientExpandResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method EntitiesClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, workspaceName string, entityID string, options *armsecurityinsights.EntitiesClientGetOptions) (resp azfake.Responder[armsecurityinsights.EntitiesClientGetResponse], errResp azfake.ErrorResponder)

	// GetInsights is the fake for method EntitiesClient.GetInsights
	// HTTP status codes to indicate success: http.StatusOK
	GetInsights func(ctx context.Context, resourceGroupName string, workspaceName string, entityID string, parameters armsecurityinsights.EntityGetInsightsParameters, options *armsecurityinsights.EntitiesClientGetInsightsOptions) (resp azfake.Responder[armsecurityinsights.EntitiesClientGetInsightsResponse], errResp azfake.ErrorResponder)

	// NewListPager is the fake for method EntitiesClient.NewListPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListPager func(resourceGroupName string, workspaceName string, options *armsecurityinsights.EntitiesClientListOptions) (resp azfake.PagerResponder[armsecurityinsights.EntitiesClientListResponse])

	// NewQueriesPager is the fake for method EntitiesClient.NewQueriesPager
	// HTTP status codes to indicate success: http.StatusOK
	NewQueriesPager func(resourceGroupName string, workspaceName string, entityID string, kind armsecurityinsights.EntityItemQueryKind, options *armsecurityinsights.EntitiesClientQueriesOptions) (resp azfake.PagerResponder[armsecurityinsights.EntitiesClientQueriesResponse])

	// RunPlaybook is the fake for method EntitiesClient.RunPlaybook
	// HTTP status codes to indicate success: http.StatusNoContent
	RunPlaybook func(ctx context.Context, resourceGroupName string, workspaceName string, entityIdentifier string, options *armsecurityinsights.EntitiesClientRunPlaybookOptions) (resp azfake.Responder[armsecurityinsights.EntitiesClientRunPlaybookResponse], errResp azfake.ErrorResponder)
}

// NewEntitiesServerTransport creates a new instance of EntitiesServerTransport with the provided implementation.
// The returned EntitiesServerTransport instance is connected to an instance of armsecurityinsights.EntitiesClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewEntitiesServerTransport(srv *EntitiesServer) *EntitiesServerTransport {
	return &EntitiesServerTransport{
		srv:             srv,
		newListPager:    newTracker[azfake.PagerResponder[armsecurityinsights.EntitiesClientListResponse]](),
		newQueriesPager: newTracker[azfake.PagerResponder[armsecurityinsights.EntitiesClientQueriesResponse]](),
	}
}

// EntitiesServerTransport connects instances of armsecurityinsights.EntitiesClient to instances of EntitiesServer.
// Don't use this type directly, use NewEntitiesServerTransport instead.
type EntitiesServerTransport struct {
	srv             *EntitiesServer
	newListPager    *tracker[azfake.PagerResponder[armsecurityinsights.EntitiesClientListResponse]]
	newQueriesPager *tracker[azfake.PagerResponder[armsecurityinsights.EntitiesClientQueriesResponse]]
}

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

func (e *EntitiesServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if entitiesServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = entitiesServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "EntitiesClient.Expand":
				res.resp, res.err = e.dispatchExpand(req)
			case "EntitiesClient.Get":
				res.resp, res.err = e.dispatchGet(req)
			case "EntitiesClient.GetInsights":
				res.resp, res.err = e.dispatchGetInsights(req)
			case "EntitiesClient.NewListPager":
				res.resp, res.err = e.dispatchNewListPager(req)
			case "EntitiesClient.NewQueriesPager":
				res.resp, res.err = e.dispatchNewQueriesPager(req)
			case "EntitiesClient.RunPlaybook":
				res.resp, res.err = e.dispatchRunPlaybook(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 (e *EntitiesServerTransport) dispatchExpand(req *http.Request) (*http.Response, error) {
	if e.srv.Expand == nil {
		return nil, &nonRetriableError{errors.New("fake for method Expand not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.OperationalInsights/workspaces/(?P<workspaceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.SecurityInsights/entities/(?P<entityId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/expand`
	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[armsecurityinsights.EntityExpandParameters](req)
	if err != nil {
		return nil, err
	}
	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
	if err != nil {
		return nil, err
	}
	workspaceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("workspaceName")])
	if err != nil {
		return nil, err
	}
	entityIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("entityId")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := e.srv.Expand(req.Context(), resourceGroupNameParam, workspaceNameParam, entityIDParam, 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).EntityExpandResponse, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (e *EntitiesServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if e.srv.Get == nil {
		return nil, &nonRetriableError{errors.New("fake for method Get not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.OperationalInsights/workspaces/(?P<workspaceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.SecurityInsights/entities/(?P<entityId>[!#&$-;=?-\[\]_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)
	}
	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
	if err != nil {
		return nil, err
	}
	workspaceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("workspaceName")])
	if err != nil {
		return nil, err
	}
	entityIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("entityId")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := e.srv.Get(req.Context(), resourceGroupNameParam, workspaceNameParam, entityIDParam, 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).EntityClassification, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (e *EntitiesServerTransport) dispatchGetInsights(req *http.Request) (*http.Response, error) {
	if e.srv.GetInsights == nil {
		return nil, &nonRetriableError{errors.New("fake for method GetInsights not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.OperationalInsights/workspaces/(?P<workspaceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.SecurityInsights/entities/(?P<entityId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/getInsights`
	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[armsecurityinsights.EntityGetInsightsParameters](req)
	if err != nil {
		return nil, err
	}
	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
	if err != nil {
		return nil, err
	}
	workspaceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("workspaceName")])
	if err != nil {
		return nil, err
	}
	entityIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("entityId")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := e.srv.GetInsights(req.Context(), resourceGroupNameParam, workspaceNameParam, entityIDParam, 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).EntityGetInsightsResponse, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (e *EntitiesServerTransport) dispatchNewListPager(req *http.Request) (*http.Response, error) {
	if e.srv.NewListPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListPager not implemented")}
	}
	newListPager := e.newListPager.get(req)
	if newListPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.OperationalInsights/workspaces/(?P<workspaceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.SecurityInsights/entities`
		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)
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		workspaceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("workspaceName")])
		if err != nil {
			return nil, err
		}
		resp := e.srv.NewListPager(resourceGroupNameParam, workspaceNameParam, nil)
		newListPager = &resp
		e.newListPager.add(req, newListPager)
		server.PagerResponderInjectNextLinks(newListPager, req, func(page *armsecurityinsights.EntitiesClientListResponse, 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) {
		e.newListPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListPager) {
		e.newListPager.remove(req)
	}
	return resp, nil
}

func (e *EntitiesServerTransport) dispatchNewQueriesPager(req *http.Request) (*http.Response, error) {
	if e.srv.NewQueriesPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewQueriesPager not implemented")}
	}
	newQueriesPager := e.newQueriesPager.get(req)
	if newQueriesPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.OperationalInsights/workspaces/(?P<workspaceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.SecurityInsights/entities/(?P<entityId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/queries`
		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)
		}
		qp := req.URL.Query()
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		workspaceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("workspaceName")])
		if err != nil {
			return nil, err
		}
		entityIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("entityId")])
		if err != nil {
			return nil, err
		}
		resp := e.srv.NewQueriesPager(resourceGroupNameParam, workspaceNameParam, entityIDParam, armsecurityinsights.EntityItemQueryKind(qp.Get("kind")), nil)
		newQueriesPager = &resp
		e.newQueriesPager.add(req, newQueriesPager)
		server.PagerResponderInjectNextLinks(newQueriesPager, req, func(page *armsecurityinsights.EntitiesClientQueriesResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newQueriesPager, req)
	if err != nil {
		return nil, err
	}
	if !slices.Contains([]int{http.StatusOK}, resp.StatusCode) {
		e.newQueriesPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newQueriesPager) {
		e.newQueriesPager.remove(req)
	}
	return resp, nil
}

func (e *EntitiesServerTransport) dispatchRunPlaybook(req *http.Request) (*http.Response, error) {
	if e.srv.RunPlaybook == nil {
		return nil, &nonRetriableError{errors.New("fake for method RunPlaybook not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.OperationalInsights/workspaces/(?P<workspaceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.SecurityInsights/entities/(?P<entityIdentifier>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/runPlaybook`
	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[armsecurityinsights.EntityManualTriggerRequestBody](req)
	if err != nil {
		return nil, err
	}
	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
	if err != nil {
		return nil, err
	}
	workspaceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("workspaceName")])
	if err != nil {
		return nil, err
	}
	entityIdentifierParam, err := url.PathUnescape(matches[regex.SubexpIndex("entityIdentifier")])
	if err != nil {
		return nil, err
	}
	var options *armsecurityinsights.EntitiesClientRunPlaybookOptions
	if !reflect.ValueOf(body).IsZero() {
		options = &armsecurityinsights.EntitiesClientRunPlaybookOptions{
			RequestBody: &body,
		}
	}
	respr, errRespr := e.srv.RunPlaybook(req.Context(), resourceGroupNameParam, workspaceNameParam, entityIdentifierParam, options)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !slices.Contains([]int{http.StatusNoContent}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusNoContent", 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 EntitiesServerTransport
var entitiesServerTransportInterceptor interface {
	// Do returns true if the server transport should use the returned response/error
	Do(*http.Request) (*http.Response, error, bool)
}
