package router

import (
	"fmt"
	"testing"

	"github.com/gin-gonic/gin"
	"github.com/stretchr/testify/require"

	"github.com/tidbcloud/infra-provider/pkg/consts"
	"github.com/tidbcloud/infra-provider/regional-server/pkg/config"
	plshandler "github.com/tidbcloud/infra-provider/regional-server/pkg/router/privatelinkservice"
)

func TestPrivateLinkServiceRoutes(t *testing.T) {
	for _, tt := range []struct {
		name     string
		provider string
	}{
		{
			name:     "AWS keeps generic PrivateLink routes only",
			provider: consts.ProviderTypeAWS,
		},
		{
			name:     "EKS adds Group address routes",
			provider: consts.ProviderTypeEKS,
		},
	} {
		t.Run(tt.name, func(t *testing.T) {
			gin.SetMode(gin.TestMode)
			engine := gin.New()
			rootConfig := config.RootConfig{ControllerConfig: config.ControllerConfig{Provider: tt.provider}}
			handler := plshandler.NewPrivateLinkServiceHandler(nil, rootConfig)
			mounts := PrivateLinkServiceMounts(tt.provider, handler)
			route(engine, nil, nil, rootConfig, config.TemplatesConfig{}, mounts...)

			routes := make(map[string]int)
			for _, info := range engine.Routes() {
				routes[fmt.Sprintf("%s|%s", info.Path, info.Method)]++
			}
			require.Equal(t, 1, routes[BasePath+"/clusters/:clusterId/privateLinkServices|POST"])
			require.Equal(t, 1, routes[BasePath+"/clusters/:clusterId/privateLinkServices/:privateLinkServiceId|DELETE"])
			require.Equal(t, 1, routes[BasePath+"/clusters/:clusterId/privateLinkServices/:privateLinkServiceId/rejectVpcEndpointConnections|POST"])
			expectedAddressRoutes := 0
			if tt.provider == consts.ProviderTypeEKS {
				expectedAddressRoutes = 1
			}
			for _, route := range []string{
				"/clusters/:clusterId/tidbgroups/:tidbGroupId/privateLinkIpAddressType|GET",
				"/clusters/:clusterId/tidbgroups/:tidbGroupId/privateLinkIpAddressType|PUT",
				"/clusters/:clusterId/tidbgroups/:tidbGroupId/validatePrivateLinkIpv4Only|POST",
			} {
				require.Equal(t, expectedAddressRoutes, routes[BasePath+route])
			}
		})
	}
}
