package repository

import (
	"testing"

	"github.com/stretchr/testify/assert"

	"github.com/tidbcloud/tidb-management-service/tidb-mgmt-service/domain/entity"
	"github.com/tidbcloud/tidb-management-service/tidb-mgmt-service/domain/repository"
)

func TestShouldForceOrgDeployPlanCountIndex(t *testing.T) {
	t.Parallel()

	t.Run("match target pattern", func(t *testing.T) {
		t.Parallel()
		filter := &repository.ClusterFilter{
			OrganizationIds: []uint64{1},
			DeployType:      entity.DeployTypeServerless,
		}
		assert.True(t, shouldForceOrgDeployPlanCountIndex(filter))
	})

	t.Run("nil filter", func(t *testing.T) {
		t.Parallel()
		assert.False(t, shouldForceOrgDeployPlanCountIndex(nil))
	})

	t.Run("multiple org ids", func(t *testing.T) {
		t.Parallel()
		filter := &repository.ClusterFilter{
			OrganizationIds: []uint64{1, 2},
			DeployType:      entity.DeployTypeServerless,
			ServicePlans:    []string{"Starter"},
		}
		assert.False(t, shouldForceOrgDeployPlanCountIndex(filter))
	})

	t.Run("missing deploy type", func(t *testing.T) {
		t.Parallel()
		filter := &repository.ClusterFilter{
			OrganizationIds: []uint64{1},
			ServicePlans:    []string{"Starter"},
		}
		assert.False(t, shouldForceOrgDeployPlanCountIndex(filter))
	})

	t.Run("with service plans", func(t *testing.T) {
		t.Parallel()
		filter := &repository.ClusterFilter{
			OrganizationIds: []uint64{1},
			DeployType:      entity.DeployTypeServerless,
			ServicePlans:    []string{"Starter", "Essential"},
		}
		assert.True(t, shouldForceOrgDeployPlanCountIndex(filter))
	})

	t.Run("query deleted", func(t *testing.T) {
		t.Parallel()
		filter := &repository.ClusterFilter{
			OrganizationIds: []uint64{1},
			DeployType:      entity.DeployTypeServerless,
			ServicePlans:    []string{"Starter"},
			QueryDeleted:    true,
		}
		assert.False(t, shouldForceOrgDeployPlanCountIndex(filter))
	})

	t.Run("with permission filter", func(t *testing.T) {
		t.Parallel()
		filter := &repository.ClusterFilter{
			OrganizationIds: []uint64{1},
			DeployType:      entity.DeployTypeServerless,
			PermissionFilter: &repository.PermissionFilter{
				All: true,
			},
		}
		assert.True(t, shouldForceOrgDeployPlanCountIndex(filter))
	})

	t.Run("with region filters", func(t *testing.T) {
		t.Parallel()
		filter := &repository.ClusterFilter{
			OrganizationIds: []uint64{1},
			DeployType:      entity.DeployTypeServerless,
			Regions:         []string{"aws-us-east-1"},
		}
		assert.True(t, shouldForceOrgDeployPlanCountIndex(filter))
	})

	t.Run("with name filter", func(t *testing.T) {
		t.Parallel()
		filter := &repository.ClusterFilter{
			OrganizationIds: []uint64{1},
			DeployType:      entity.DeployTypeServerless,
			Name:            "test",
		}
		assert.True(t, shouldForceOrgDeployPlanCountIndex(filter))
	})

	t.Run("with labels filter", func(t *testing.T) {
		t.Parallel()
		filter := &repository.ClusterFilter{
			OrganizationIds: []uint64{1},
			DeployType:      entity.DeployTypeServerless,
			Labels:          map[string]string{"k": "v"},
		}
		assert.True(t, shouldForceOrgDeployPlanCountIndex(filter))
	})
}
