package datasource

import (
	"context"
	"testing"

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

func TestShouldSkipProjectDatasourceQuery(t *testing.T) {
	tests := []struct {
		name     string
		info     TidbClusterInfo
		expected bool
	}{
		{
			name:     "premium should skip with project id",
			info:     TidbClusterInfo{ServicePlan: "Premium", ProjectID: entity.ID(123)},
			expected: true,
		},
		{
			name:     "essential v2 should skip with project id",
			info:     TidbClusterInfo{ServicePlan: "Essential_V2", ProjectID: entity.ID(123)},
			expected: true,
		},
		{
			name:     "essential with project id should skip",
			info:     TidbClusterInfo{ServicePlan: "Essential", ProjectID: entity.ID(123)},
			expected: true,
		},
		{
			name:     "starter with project id should skip",
			info:     TidbClusterInfo{ServicePlan: "Starter", ProjectID: entity.ID(123)},
			expected: true,
		},
		{
			name:     "empty project id should skip",
			info:     TidbClusterInfo{ProjectID: entity.ID(0)},
			expected: true,
		},
		{
			name:     "empty servicePlan with project id treated as dedicated should not skip",
			info:     TidbClusterInfo{ServicePlan: "", ProjectID: entity.ID(123)},
			expected: false,
		},
		{
			name:     "empty servicePlan without project id treated as dedicated should skip",
			info:     TidbClusterInfo{ServicePlan: "", ProjectID: entity.ID(0)},
			expected: true,
		},
		{
			name:     "dedicated with project id should not skip",
			info:     TidbClusterInfo{ServicePlan: "Dedicated", ProjectID: entity.ID(123)},
			expected: false,
		},
		{
			name:     "dedicated without project id should skip",
			info:     TidbClusterInfo{ServicePlan: "Dedicated", ProjectID: entity.ID(0)},
			expected: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := shouldSkipProjectDatasourceQuery(tt.info); got != tt.expected {
				t.Fatalf("shouldSkipProjectDatasourceQuery() = %v, want %v", got, tt.expected)
			}
		})
	}
}

func TestGetMetricDataAddressFromContext_AllowEmptyProjectQueryURL(t *testing.T) {
	addr := MetricDataAddress{
		QueryUrl:        "http://example.com",
		ProjectQueryUrl: "",
	}
	ctx := context.WithValue(context.Background(), metricDataAddressKey, addr)

	got, err := GetMetricDataAddressFromContext(ctx)
	if err != nil {
		t.Fatalf("GetMetricDataAddressFromContext() error = %v", err)
	}
	if got.QueryUrl == "" {
		t.Fatalf("QueryUrl should not be empty")
	}
}

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

	config.Global().O11yGlobalServerConfig = config.O11yGlobalServerConfig{
		Url:                      "https://global.example.com",
		LocalTestEnabled:         true,
		LocalTestGlobalServerURL: "http://127.0.0.1:19080",
	}

	if got := getO11yGlobalServerURL(); got != "http://127.0.0.1:19080" {
		t.Fatalf("getO11yGlobalServerURL() = %q, want %q", got, "http://127.0.0.1:19080")
	}
}

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

	config.Global().O11yGlobalServerConfig = config.O11yGlobalServerConfig{
		LocalTestEnabled:           true,
		LocalTestRegionalServerURL: "http://127.0.0.1:19081",
	}

	got := rewriteRegionalServerURL("https://www.ds.us-east-1.aws.observability.tidbcloud.com/internal/metrics/cluster-id/api/v1/")
	want := "http://127.0.0.1:19081/internal/metrics/cluster-id/api/v1/"
	if got != want {
		t.Fatalf("rewriteRegionalServerURL() = %q, want %q", got, want)
	}
}
