package pipeline

import (
	"testing"

	"github.com/tidbcloud/metrics-integration/global/internal/domain/entity"
	"github.com/tidbcloud/metrics-integration/global/internal/infrastructure/config"
)

func TestPipelineConfig_ToHelmValues_BackfillBatchMode(t *testing.T) {
	original := *config.Global()
	t.Cleanup(func() {
		*config.Global() = original
	})

	config.Global().DebugConfig.DisableDedicatedNodepool = true

	tests := []struct {
		name    string
		enabled bool
	}{
		{name: "enabled", enabled: true},
		{name: "disabled", enabled: false},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			cfg := &PipelineConfig{
				TargetCluster: TargetCluster{
					Id:                "1001",
					Name:              "cluster-a",
					CloudProviderName: "aws",
					RegionName:        "us-west-2",
					TenantId:          "2001",
					ProjectId:         "3001",
					ProjectName:       "project-a",
					ServicePlan:       "Dedicated",
				},
				PipelineMetaData: &entity.Integration{
					ProviderType: entity.IntegrationTypeDatadog,
					DatadogSite:  entity.DatadogSiteUS1,
				},
				DecryptedAPIKey:         "test-key",
				O11yGlobalServerConfig:  map[string]interface{}{"url": "https://example.com"},
				EnableBackfillBatchMode: tt.enabled,
			}

			values := cfg.ToHelmValues()
			pipelineValues, ok := values["pipeline"].(map[string]interface{})
			if !ok {
				t.Fatalf("pipeline values missing or wrong type: %#v", values["pipeline"])
			}

			backfillValues, ok := pipelineValues["backfill"].(map[string]interface{})
			if !ok {
				t.Fatalf("backfill values missing or wrong type: %#v", pipelineValues["backfill"])
			}

			if got, ok := backfillValues["batchModeEnabled"].(bool); !ok || got != tt.enabled {
				t.Fatalf("batchModeEnabled = %#v, want %v", backfillValues["batchModeEnabled"], tt.enabled)
			}
		})
	}
}
