// 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/netapp/armnetapp/v10"
	"net/http"
	"net/url"
	"regexp"
	"slices"
)

// BackupsUnderAccountServer is a fake server for instances of the armnetapp.BackupsUnderAccountClient type.
type BackupsUnderAccountServer struct {
	// BeginMigrateBackups is the fake for method BackupsUnderAccountClient.BeginMigrateBackups
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginMigrateBackups func(ctx context.Context, resourceGroupName string, accountName string, body armnetapp.BackupsMigrationRequest, options *armnetapp.BackupsUnderAccountClientBeginMigrateBackupsOptions) (resp azfake.PollerResponder[armnetapp.BackupsUnderAccountClientMigrateBackupsResponse], errResp azfake.ErrorResponder)
}

// NewBackupsUnderAccountServerTransport creates a new instance of BackupsUnderAccountServerTransport with the provided implementation.
// The returned BackupsUnderAccountServerTransport instance is connected to an instance of armnetapp.BackupsUnderAccountClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewBackupsUnderAccountServerTransport(srv *BackupsUnderAccountServer) *BackupsUnderAccountServerTransport {
	return &BackupsUnderAccountServerTransport{
		srv:                 srv,
		beginMigrateBackups: newTracker[azfake.PollerResponder[armnetapp.BackupsUnderAccountClientMigrateBackupsResponse]](),
	}
}

// BackupsUnderAccountServerTransport connects instances of armnetapp.BackupsUnderAccountClient to instances of BackupsUnderAccountServer.
// Don't use this type directly, use NewBackupsUnderAccountServerTransport instead.
type BackupsUnderAccountServerTransport struct {
	srv                 *BackupsUnderAccountServer
	beginMigrateBackups *tracker[azfake.PollerResponder[armnetapp.BackupsUnderAccountClientMigrateBackupsResponse]]
}

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

func (b *BackupsUnderAccountServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if backupsUnderAccountServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = backupsUnderAccountServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "BackupsUnderAccountClient.BeginMigrateBackups":
				res.resp, res.err = b.dispatchBeginMigrateBackups(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 (b *BackupsUnderAccountServerTransport) dispatchBeginMigrateBackups(req *http.Request) (*http.Response, error) {
	if b.srv.BeginMigrateBackups == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginMigrateBackups not implemented")}
	}
	beginMigrateBackups := b.beginMigrateBackups.get(req)
	if beginMigrateBackups == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.NetApp/netAppAccounts/(?P<accountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/migrateBackups`
		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[armnetapp.BackupsMigrationRequest](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		accountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("accountName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := b.srv.BeginMigrateBackups(req.Context(), resourceGroupNameParam, accountNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginMigrateBackups = &respr
		b.beginMigrateBackups.add(req, beginMigrateBackups)
	}

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

	if !slices.Contains([]int{http.StatusOK, http.StatusAccepted, http.StatusNoContent}, resp.StatusCode) {
		b.beginMigrateBackups.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(beginMigrateBackups) {
		b.beginMigrateBackups.remove(req)
	}

	return resp, nil
}

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