package bootstrapimpl

import (
	"testing"

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

func TestRuntimePlatformCommandPublishesNullableDependencyState(t *testing.T) {
	definitions := GetManagedTableDefinitions()
	var command *schema.TableDefinition
	for index := range definitions {
		if definitions[index].TableName == "_System_RuntimePlatformCommand" {
			command = &definitions[index]
			break
		}
	}
	if command == nil {
		t.Fatal("_System_RuntimePlatformCommand is not published")
	}

	want := map[string]string{
		"DependencyKind":                  "VARCHAR(64)",
		"DependencyObjectAPIName":         "VARCHAR(255)",
		"DependencyRecordId":              "VARCHAR(255)",
		"DependencyKeyHash":               "CHAR(64)",
		"DependencyConnectorOwnerKey":     "VARCHAR(255)",
		"DependencyConnectorNamespace":    "VARCHAR(255)",
		"DependencyConnectorSourceObject": "VARCHAR(255)",
		"DependencyDestinationSlotId":     "VARCHAR(255)",
		"DependencyConnectorRemoteId":     "VARCHAR(255)",
		"DependencyBlockedAt":             "DATETIME(6)",
	}
	for _, column := range command.Columns {
		columnType, required := want[column.Name]
		if !required {
			continue
		}
		if column.Type != columnType || !column.Nullable {
			t.Fatalf(
				"column %s = type:%q nullable:%t, want type:%q nullable:true",
				column.Name,
				column.Type,
				column.Nullable,
				columnType,
			)
		}
		delete(want, column.Name)
	}
	if len(want) != 0 {
		t.Fatalf("Runtime Platform command dependency columns are missing: %v", want)
	}
}
