package configs_test

import (
	"os"
	"regexp"
	"testing"

	"github.com/stretchr/testify/require"
	"sigs.k8s.io/yaml"
)

type domains struct {
	Prod []string `yaml:"prod"`
}

type namespace struct {
	Prod string `yaml:"prod"`
}

type service struct {
	ServiceName string   `yaml:"serviceName"`
	Regex       []string `yaml:"regex"`
}

type ingress struct {
	Namespace namespace `yaml:"namespace"`
	Services  []service `yaml:"services"`
}

type config struct {
	Domains           domains    `yaml:"domains"`
	IngressNamePrefix string     `yaml:"ingressNamePrefix"`
	Ingresses         []ingress  `yaml:"ingresses"`
}

func TestFormalConfigMatchesVpcPeeringIDsWithHyphens(t *testing.T) {
	configFile, err := os.ReadFile("formal.yaml")
	require.NoError(t, err)

	var configs []config
	require.NoError(t, yaml.Unmarshal(configFile, &configs))

	const vpcPeeringPath = "/api/v2025/cloud/v1beta2/byoc/tidbs/10082742805014456847/vpcPeerings/pcx-09440ba937dcfc4e7"

	for _, cfg := range configs {
		for _, ing := range cfg.Ingresses {
			for _, svc := range ing.Services {
				if svc.ServiceName != "nextgen-tidb-mgmt-portal-ui" {
					continue
				}
				for _, pathRegex := range svc.Regex {
					re, err := regexp.Compile("^" + pathRegex)
					require.NoError(t, err)
					if re.MatchString(vpcPeeringPath) {
						return
					}
				}
			}
		}
	}

	t.Fatalf("no regex in formal.yaml matched %s", vpcPeeringPath)
}
