package prometheus

import (
	"strings"
	"testing"

	"github.com/tidbcloud/metrics-integration/pipeline/central/config"
)

func TestNormalizeClusterConfig(t *testing.T) {
	t.Run("normalizes service plan and components", func(t *testing.T) {
		targetCluster := &config.TargetCluster{
			ServicePlan: "premium",
			Components: []config.ClusterComponent{
				{ComponentType: "component_type_tidb", CpuNum: 2},
				{ComponentType: "COMPONENT_TYPE_TICDC", CpuNum: 4},
				{ComponentType: "COMPONENT_TYPE_TIPROXY", CpuNum: 6},
			},
		}

		err := normalizeClusterConfig(42, "cm-42", targetCluster)
		if err != nil {
			t.Fatalf("normalizeClusterConfig() error = %v", err)
		}
		if targetCluster.ServicePlan != "Premium" {
			t.Fatalf("ServicePlan = %q, want Premium", targetCluster.ServicePlan)
		}
		if len(targetCluster.Components) != 1 {
			t.Fatalf("len(Components) = %d, want 1", len(targetCluster.Components))
		}
		if targetCluster.Components[0].ComponentType != "COMPONENT_TYPE_TIDB" {
			t.Fatalf("Components[0].ComponentType = %q, want COMPONENT_TYPE_TIDB", targetCluster.Components[0].ComponentType)
		}
	})

	t.Run("returns contextual error for invalid service plan", func(t *testing.T) {
		targetCluster := &config.TargetCluster{ServicePlan: "BYOC"}

		err := normalizeClusterConfig(42, "cm-42", targetCluster)
		if err == nil {
			t.Fatal("expected normalizeClusterConfig() to fail")
		}
		if !strings.Contains(err.Error(), `servicePlan "BYOC"`) {
			t.Fatalf("error = %v, want service plan value", err)
		}
		if !strings.Contains(err.Error(), "clusterID 42") {
			t.Fatalf("error = %v, want cluster id context", err)
		}
		if !strings.Contains(err.Error(), "ConfigMap cm-42") {
			t.Fatalf("error = %v, want ConfigMap context", err)
		}
	})
}
