package runtimeworker

import (
	"strings"
	"testing"

	"github.com/nexuscrm/kernel/pkg/schemacatalog"
)

// workerScratchSystemTables is the complete canonical _System_* physical-table
// closure used by the real-TiDB Runtime Worker integration fixture. It is not
// a claim that every L0 primitive has that prefix: generic identity primitives
// such as UserRole are not system tables. A non-system table must instead be
// declared by an actual tested work path or its bound RuntimeMetadataView, not
// injected here merely to silence an eager legacy probe.
func workerScratchSystemTables() map[string]bool {
	return map[string]bool{
		"_System_AuditLog":                         true,
		"_System_AuditOutbox":                      true,
		"_System_ConnectorInboundRemoteIDEvidence": true,
		"_System_ConnectorPushAttempt":             true,
		"_System_ConnectorRemoteIdentityLane":      true,
		"_System_ConnectorRemoteLink":              true,
		"_System_Field":                            true,
		"_System_FieldHistorySequence":             true,
		"_System_File":                             true,
		"_System_FileUploadOperation":              true,
		"_System_ParentLane":                       true,
		"_System_LayoutAssignment":                 true,
		"_System_Object":                           true,
		"_System_PageLayout":                       true,
		"_System_PageLayoutHistory":                true,
		"_System_PermissionStateVersion":           true,
		"_System_RecordType":                       true,
		"_System_RecordEventOutbox":                true,
		"_System_RollupDelta":                      true,
		"_System_RuntimePlatformCommand":           true,
		"_System_RuntimeOutboundIntent":            true,
		"_System_RuntimeOutboundIntentGeneration":  true,
		"_System_RuntimeRecordCommand":             true,
		"_System_RuntimeRecordCommandOperation":    true,
		"_System_RuntimeWorkFence":                 true,
		"_System_RuntimeWorkOwnership":             true,
		"_System_ScheduledRunReservation":          true,
	}
}

func TestWorkerScratchSystemTablesAreCanonicalAndAppBlind(t *testing.T) {
	definitions, err := schemacatalog.SystemTableDefinitions()
	if err != nil {
		t.Fatalf("load canonical system table definitions: %v", err)
	}
	known := make(map[string]struct{}, len(definitions))
	for _, definition := range definitions {
		known[definition.TableName] = struct{}{}
	}
	for tableName := range workerScratchSystemTables() {
		if !strings.HasPrefix(tableName, "_System_") {
			t.Errorf("worker scratch schema includes non-system table %q; product objects belong in the RuntimeMetadataView fixture", tableName)
		}
		if _, ok := known[tableName]; !ok {
			t.Errorf("worker scratch schema requests unknown canonical system table %q", tableName)
		}
	}
}
