package bootstrapimpl

import (
	"testing"
)

// TestConnectorRateLimitTableExists asserts that _System_ConnectorRateLimit is
// declared in system_tables.json and carries the expected key columns.
func TestConnectorRateLimitTableExists(t *testing.T) {
	const tableName = "_System_ConnectorRateLimit"
	defs := GetManagedTableDefinitions()

	var found bool
	for _, def := range defs {
		if def.TableName != tableName {
			continue
		}
		found = true

		wantCols := []string{
			"Id", "OwnerPack", "EndpointClass",
			"RequestsPerSecond", "Burst", "IsDeleted",
			"CreatedDate", "LastModifiedDate",
		}
		colIdx := make(map[string]bool, len(def.Columns))
		for _, col := range def.Columns {
			colIdx[col.Name] = true
		}
		for _, name := range wantCols {
			if !colIdx[name] {
				t.Errorf("%s: missing expected column %q", tableName, name)
			}
		}
		break
	}

	if !found {
		t.Fatalf("%s not found in system_tables.json", tableName)
	}
}

// TestConnectorRateLimitRequiredReadPermissionIsBlocked pins requiredReadPermission
// to "BLOCKED". Generic SObject CRUD/query paths must not read limit policy rows
// directly; the only valid read path is features/connector/throttle.LimitProvider.
func TestConnectorRateLimitRequiredReadPermissionIsBlocked(t *testing.T) {
	const (
		tableName = "_System_ConnectorRateLimit"
		wantPerm  = "BLOCKED"
	)
	defs := GetManagedTableDefinitions()
	for _, def := range defs {
		if def.TableName != tableName {
			continue
		}
		if def.RequiredReadPermission != wantPerm {
			t.Fatalf(
				"%s.requiredReadPermission = %q, want %q; "+
					"rate-limit policy rows must not be readable via generic CRUD. "+
					"Read path is connector_throttle.LimitProvider only.",
				tableName, def.RequiredReadPermission, wantPerm,
			)
		}
		return
	}
	t.Fatalf("%s not found in system_tables.json", tableName)
}

// TestConnectorRateLimitRequiredWritePermissionIsBlocked pins requiredWritePermission
// to "BLOCKED". Direct writes would bypass connector pack validation and could inject
// arbitrary rate-limit policy outside the pack-install pipeline.
func TestConnectorRateLimitRequiredWritePermissionIsBlocked(t *testing.T) {
	const (
		tableName = "_System_ConnectorRateLimit"
		wantPerm  = "BLOCKED"
	)
	defs := GetManagedTableDefinitions()
	for _, def := range defs {
		if def.TableName != tableName {
			continue
		}
		if def.RequiredWritePermission != wantPerm {
			t.Fatalf(
				"%s.requiredWritePermission = %q, want %q; "+
					"direct writes bypass connector pack validation (NCRM-INT-04B). "+
					"Write path is ConnectorRateLimitComponent.Apply only.",
				tableName, def.RequiredWritePermission, wantPerm,
			)
		}
		return
	}
	t.Fatalf("%s not found in system_tables.json", tableName)
}

// TestConnectorRateLimitUniqueIndex asserts that _System_ConnectorRateLimit
// declares the unique index on (OwnerPack, EndpointClass).
func TestConnectorRateLimitUniqueIndex(t *testing.T) {
	const tableName = "_System_ConnectorRateLimit"
	defs := GetManagedTableDefinitions()
	for _, def := range defs {
		if def.TableName != tableName {
			continue
		}
		for _, idx := range def.Indices {
			if !idx.Unique {
				continue
			}
			if len(idx.Columns) == 2 &&
				idx.Columns[0] == "OwnerPack" &&
				idx.Columns[1] == "EndpointClass" {
				return // found
			}
		}
		t.Fatalf("%s: unique index on (OwnerPack, EndpointClass) not found; declared indices: %v",
			tableName, func() []string {
				var out []string
				for _, idx := range def.Indices {
					out = append(out, func() string {
						if idx.Unique {
							return "unique" + idx.Name
						}
						return "idx" + idx.Name
					}())
				}
				return out
			}())
	}
	t.Fatalf("%s not found in system_tables.json", tableName)
}
