package service

import (
	"strings"
	"testing"

	"github.com/pingcap-inc/clinic/clinic_data_service/pkg/engine"
)

func TestResolveTopSQLDataSource(t *testing.T) {
	baseURL := "s3://o11y-prod-shared-us-east-1/deltalake/org=1372813089209061633/cluster=10989049060142230334/type=topsql/"

	tests := []struct {
		name      string
		base      string
		tableName string
		expected  string
	}{
		{
			name:      "topsql_tidb with cluster suffix",
			base:      baseURL,
			tableName: "topsql_tidb_10989049060142230334",
			expected:  "s3://o11y-prod-shared-us-east-1/deltalake/org=1372813089209061633/cluster=10989049060142230334/type=topsql/component=tidb/",
		},
		{
			name:      "topsql_tikv with cluster suffix",
			base:      baseURL,
			tableName: "topsql_tikv_10989049060142230334",
			expected:  "s3://o11y-prod-shared-us-east-1/deltalake/org=1372813089209061633/cluster=10989049060142230334/type=topsql/component=tikv/",
		},
		{
			name:      "topsql_plan_meta with cluster suffix",
			base:      baseURL,
			tableName: "topsql_plan_meta_10989049060142230334",
			expected:  "s3://o11y-prod-shared-us-east-1/deltalake/org=1372813089209061633/cluster=10989049060142230334/type=topsql/component=topsql_plan_meta/",
		},
		{
			name:      "topsql_sql_meta with cluster suffix",
			base:      baseURL,
			tableName: "topsql_sql_meta_10989049060142230334",
			expected:  "s3://o11y-prod-shared-us-east-1/deltalake/org=1372813089209061633/cluster=10989049060142230334/type=topsql/component=topsql_sql_meta/",
		},
		{
			name:      "topsql_topru with cluster suffix",
			base:      baseURL,
			tableName: "topsql_topru_10989049060142230334",
			expected:  "s3://o11y-prod-shared-us-east-1/deltalake/org=1372813089209061633/cluster=10989049060142230334/type=topsql/component=topru/",
		},
		{
			name:      "slow_query_logs unchanged",
			base:      "s3://bucket/path/type=slow_query/",
			tableName: "slow_query_logs_123456",
			expected:  "s3://bucket/path/type=slow_query/",
		},
		{
			name:      "base URL without trailing slash",
			base:      "s3://bucket/path/type=topsql",
			tableName: "topsql_tidb_123456",
			expected:  "s3://bucket/path/type=topsql/component=tidb/",
		},
		{
			name:      "topsql without sub-table (just topsql_clusterid)",
			base:      baseURL,
			tableName: "topsql_10989049060142230334",
			expected:  baseURL + "component=tidb/",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := engine.ResolveTopSQLDataSource(tt.base, tt.tableName)
			if got != tt.expected {
				t.Errorf("ResolveTopSQLDataSource(%q, %q)\n  got:  %q\n  want: %q", tt.base, tt.tableName, got, tt.expected)
			}
		})
	}
}

func TestPhysicalTableNameMapIncludesStatementsWithoutChangingSlowQuery(t *testing.T) {
	tableMap := physicalTableNameMap("123456", "s3://bucket/path/persisted_statements_summary/")
	if got := tableMap["statements"]; !strings.HasPrefix(got, "statements_123456_") {
		t.Fatalf("statements physical table = %q, want source-bound prefix", got)
	}
	if got := tableMap["slow_query_logs"]; got != "slow_query_logs_123456" {
		t.Fatalf("slow query physical table = %q, want %q", got, "slow_query_logs_123456")
	}
}

func TestStatementsPhysicalTableNameIsBoundToDataSource(t *testing.T) {
	first := statementsPhysicalTableName("123456", "s3://bucket/path-a/")
	if first != statementsPhysicalTableName("123456", "s3://bucket/path-a") {
		t.Fatal("trailing slash should not change Statements table identity")
	}
	if first == statementsPhysicalTableName("123456", "s3://bucket/path-b") {
		t.Fatal("different authoritative sources must use different physical tables")
	}
}

func TestQueryReferencesLogicalStatementsTable(t *testing.T) {
	if !queryReferencesLogicalTable(`SELECT digest FROM clinic_data_proxy.statements WHERE date = '20260721'`, "123", "statements") {
		t.Fatal("logical statements reference was not detected")
	}
	if !queryReferencesLogicalTable(`SELECT digest FROM statements_123 WHERE date = '20260721'`, "123", "statements") {
		t.Fatal("physical statements reference was not detected")
	}
	if !queryReferencesLogicalTable(`SELECT digest FROM statements_123_deadbeef WHERE date = '20260721'`, "123", "statements") {
		t.Fatal("source-bound physical statements reference was not detected")
	}
	if queryReferencesLogicalTable(`SELECT digest FROM slow_query_logs WHERE date = '20260721'`, "123", "statements") {
		t.Fatal("slow query must not be detected as statements")
	}
}
