package task

import (
	"testing"

	"github.com/tidbcloud/devops/pkg/infra/config"
)

func TestResolveMaintenancePolicy(t *testing.T) {
	gate := config.ScopedFeatureGate{
		Tenants:  []string{"tenant-1"},
		Projects: []string{"project-1"},
	}

	tests := []struct {
		name       string
		tenantID   string
		projectID  string
		want       string
		gate       config.ScopedFeatureGate
	}{
		{
			name:      "all enabled",
			gate:      config.ScopedFeatureGate{All: true},
			tenantID:  "any",
			projectID: "any",
			want:      MaintenancePolicyModeClusterMaintenance,
		},
		{
			name:      "tenant match",
			gate:      gate,
			tenantID:  "tenant-1",
			projectID: "other",
			want:      MaintenancePolicyModeClusterMaintenance,
		},
		{
			name:      "project match",
			gate:      gate,
			tenantID:  "other",
			projectID: "project-1",
			want:      MaintenancePolicyModeClusterMaintenance,
		},
		{
			name:      "legacy default",
			gate:      gate,
			tenantID:  "other",
			projectID: "other",
			want:      MaintenancePolicyModeLegacy,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := ResolveMaintenancePolicy(tt.gate, tt.tenantID, tt.projectID); got != tt.want {
				t.Fatalf("ResolveMaintenancePolicy() = %q, want %q", got, tt.want)
			}
		})
	}
}
