package configs

import (
	"io/fs"
	"os"
	"testing"

	"gopkg.in/yaml.v3"

	"github.com/stretchr/testify/assert"

	. "github.com/onsi/gomega"
	"github.com/tidbcloud/observability/pkg/common/structs"
)

func TestLoadAZConfig(t *testing.T) {
	t.Run("normal", func(t *testing.T) {
		g := NewGomegaWithT(t)
		azconfig := make(map[string]structs.AvailabilityZoneConfig)
		azconfig["us-west-2a"] = structs.AvailabilityZoneConfig{
			EndpointServiceID:    "vpce-svc-02dd94106458bccae",
			IngressClassName:     "nginx-az1",
			DnsName:              "promethues-enpoint-01.shared.aws.tidbcloud.com",
			ReadIngressClassName: "empty",
		}
		azconfig["us-west-2b"] = structs.AvailabilityZoneConfig{
			EndpointServiceID:    "vpce-svc-02dd94106458bccae",
			IngressClassName:     "nginx-az1",
			DnsName:              "promethues-enpoint-01.shared.aws.tidbcloud.com",
			ReadIngressClassName: "empty",
		}
		azconfig["us-west-2c"] = structs.AvailabilityZoneConfig{
			EndpointServiceID:    "vpce-svc-084eed90e91133d08",
			IngressClassName:     "nginx-az2",
			DnsName:              "promethues-enpoint-02.shared.aws.tidbcloud.com",
			ReadIngressClassName: "empty",
		}
		azconfig["us-west-2d"] = structs.AvailabilityZoneConfig{
			EndpointServiceID:    "vpce-svc-084eed90e91133d08",
			IngressClassName:     "nginx-az2",
			DnsName:              "promethues-enpoint-02.shared.aws.tidbcloud.com",
			ReadIngressClassName: "empty",
		}

		content, err := yaml.Marshal(azconfig)
		assert.NoError(t, err)
		path := "test_azconfig.yaml"
		os.WriteFile(path, content, fs.ModePerm)
		defer os.Remove(path)

		g.Expect(LoadAZConfig(path)).Should(Equal(azconfig))
	})
	t.Run("invalid file", func(t *testing.T) {
		assert.Panics(t, func() {
			LoadAZConfig("invalid")
		})
	})
}
