package nodepoolgc

import (
	"testing"

	"github.com/stretchr/testify/require"
	"k8s.io/apimachinery/pkg/labels"

	cloudv1 "github.com/tidbcloud/infra-api/api/v1"
	"github.com/tidbcloud/infra-provider/pkg/consts"
	"github.com/tidbcloud/infra-provider/pkg/namer"
)

func Test_buildListBRNodePoolsSelector(t *testing.T) {
	selector, err := buildListBRNodePoolsSelector()
	require.NoError(t, err)

	tests := []struct {
		name     string
		labelSet labels.Set
		matched  bool
	}{
		{
			name: "matches backup component nodepool",
			labelSet: labels.Set{
				consts.TagKeyServiceType:   consts.ServiceTypeDedicated,
				consts.TagKeyUsedBy:        consts.UsedByCustomerTiDB,
				namer.ComponentSelectorKey: string(cloudv1.ComponentBackup),
			},
			matched: true,
		},
		{
			name: "matches restore component nodepool",
			labelSet: labels.Set{
				consts.TagKeyServiceType:   consts.ServiceTypeDedicated,
				consts.TagKeyUsedBy:        consts.UsedByCustomerTiDB,
				namer.ComponentSelectorKey: string(cloudv1.ComponentRestore),
			},
			matched: true,
		},
		{
			name: "matches compact backup component nodepool",
			labelSet: labels.Set{
				consts.TagKeyServiceType:   consts.ServiceTypeDedicated,
				consts.TagKeyUsedBy:        consts.UsedByCustomerTiDB,
				namer.ComponentSelectorKey: string(cloudv1.ComponentCompactBackup),
			},
			matched: true,
		},
		{
			name: "does not match TiDB component",
			labelSet: labels.Set{
				consts.TagKeyServiceType:   consts.ServiceTypeDedicated,
				consts.TagKeyUsedBy:        consts.UsedByCustomerTiDB,
				namer.ComponentSelectorKey: string(cloudv1.ComponentTiDB),
			},
			matched: false,
		},
		{
			name: "does not match Premium service type",
			labelSet: labels.Set{
				consts.TagKeyServiceType:   consts.ServiceTypePremium,
				consts.TagKeyUsedBy:        consts.UsedByCustomerTiDB,
				namer.ComponentSelectorKey: string(cloudv1.ComponentBackup),
			},
			matched: false,
		},
		{
			name: "does not match Seed workload",
			labelSet: labels.Set{
				consts.TagKeyServiceType:   consts.ServiceTypeDedicated,
				consts.TagKeyUsedBy:        consts.UsedBySeed,
				namer.ComponentSelectorKey: string(cloudv1.ComponentBackup),
			},
			matched: false,
		},
		{
			name: "does not match missing component label",
			labelSet: labels.Set{
				consts.TagKeyServiceType: consts.ServiceTypeDedicated,
				consts.TagKeyUsedBy:      consts.UsedByCustomerTiDB,
			},
			matched: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			require.Equal(t, tt.matched, selector.Matches(tt.labelSet))
		})
	}
}
