package spu

import (
	"fmt"
	"testing"

	"github.com/smartystreets/goconvey/convey"

	globalcommonv1 "github.com/tidbcloud/tidb-management-service/api/spec/global/common/v1"
	globaldedicatedv1 "github.com/tidbcloud/tidb-management-service/api/spec/global/dedicated/v1"
	"github.com/tidbcloud/tidb-management-service/dedicated-global-service/domain/entity"
)

func TestLabelBuilder(t *testing.T) {
	convey.Convey("Test_TierLabelBuilder", t, func() {
		tierKey := &entity.Spu{
			Id:            10,
			Key:           "2C8G",
			ComponentType: globaldedicatedv1.ComponentType_TIDB,
		}
		tenantId := uint64(123)
		provider := globalcommonv1.CloudProvider_AWS
		region := globalcommonv1.Region_AWS_US_EAST_1
		cpuArch := globaldedicatedv1.InstanceMu_ARM64

		convey.Convey("ok", func() {
			labels, err := newLabelBuilder().WithTenant(tenantId).
				WithComponent(tierKey.ComponentType).
				WithSpu(tierKey.Id).
				WithProvider(provider).
				WithRegion(region).
				WithCpuArch(cpuArch).
				Build()
			convey.So(err, convey.ShouldBeNil)
			convey.So(
				labels.Get(globaldedicatedv1.Sku_Condition_TIER_ID),
				convey.ShouldEqual,
				fmt.Sprintf("%d", tierKey.Id),
			)
			convey.So(
				labels.Get(globaldedicatedv1.Sku_Condition_TENANT),
				convey.ShouldEqual,
				fmt.Sprintf("%d", tenantId),
			)
			convey.So(labels.Get(globaldedicatedv1.Sku_Condition_PROVIDER), convey.ShouldEqual, provider.String())
			convey.So(labels.Get(globaldedicatedv1.Sku_Condition_REGION), convey.ShouldEqual, region.String())
			convey.So(
				labels.Get(globaldedicatedv1.Sku_Condition_COMPONENT),
				convey.ShouldEqual,
				tierKey.ComponentType.String(),
			)
			convey.So(labels.Get(globaldedicatedv1.Sku_Condition_CPUARCH), convey.ShouldEqual, cpuArch.String())
		})
		convey.Convey("no tenant id", func() {
			_, err := newLabelBuilder().WithComponent(tierKey.ComponentType).
				WithSpu(tierKey.Id).
				WithProvider(provider).
				WithRegion(region).
				WithCpuArch(cpuArch).
				Build()
			convey.So(err, convey.ShouldBeError)
		})

		convey.Convey("no provider id", func() {
			_, err := newLabelBuilder().WithTenant(tenantId).WithRegion(region).WithCpuArch(cpuArch).Build()
			convey.So(err, convey.ShouldBeError)
		})

		convey.Convey("no region", func() {
			_, err := newLabelBuilder().WithProvider(provider).WithTenant(tenantId).Build()
			convey.So(err, convey.ShouldBeError)
		})
	})
}
