extends SceneTree

## Headless M01 contract verification for non-interchangeable identity values.

const ContentIdScript = preload("res://src/domain/identity/content_id.gd")
const EntityIdScript = preload("res://src/domain/identity/entity_id.gd")
const WowGuidScript = preload("res://src/domain/identity/wow_guid.gd")
const ServerEntryIdScript = preload("res://src/domain/identity/server_entry_id.gd")


func _initialize() -> void:
	var failures: Array[String] = []
	_verify_content_id(failures)
	_verify_entity_id(failures)
	_verify_wow_guid(failures)
	_verify_server_entry_id(failures)
	_verify_scene_free_sources(failures)

	if not failures.is_empty():
		for failure in failures:
			push_error("DOMAIN_IDENTITIES: %s" % failure)
		quit(1)
		return

	print("DOMAIN_IDENTITIES PASS content=5 entity=5 wow_guid=6 server_entry=5")
	quit(0)


func _verify_content_id(failures: Array[String]) -> void:
	var normalized = ContentIdScript.new(" 123E4567-E89B-42D3-A456-426614174000 ")
	_expect_true(normalized.is_valid(), "normalized ContentId is valid", failures)
	_expect_equal(normalized.to_key(), "123e4567-e89b-42d3-a456-426614174000", "ContentId canonical key", failures)
	_expect_true(normalized.equals(ContentIdScript.new("123e4567-e89b-42d3-a456-426614174000")), "equal ContentIds", failures)
	_expect_true(not normalized.equals(ContentIdScript.new("123e4567-e89b-42d3-a456-426614174001")), "different ContentIds", failures)
	_expect_true(not ContentIdScript.new("123e4567e89b42d3a456426614174000").is_valid(), "malformed ContentId", failures)


func _verify_entity_id(failures: Array[String]) -> void:
	var entity_id = EntityIdScript.new(42)
	_expect_true(entity_id.is_valid(), "positive EntityId", failures)
	_expect_true(not EntityIdScript.new(0).is_valid(), "zero EntityId", failures)
	_expect_true(not EntityIdScript.new(-1).is_valid(), "negative EntityId", failures)
	_expect_true(entity_id.equals(EntityIdScript.new(42)), "equal EntityIds", failures)
	_expect_equal(entity_id.to_debug_key(), "entity:42", "EntityId diagnostic key", failures)


func _verify_wow_guid(failures: Array[String]) -> void:
	var guid = WowGuidScript.new(0xf1300000, 0xffffffff)
	_expect_true(guid.has_valid_word_ranges(), "maximum GUID low word", failures)
	_expect_equal(guid.to_hex_string(), "f1300000ffffffff", "GUID hexadecimal form", failures)
	_expect_true(guid.equals(WowGuidScript.new(0xf1300000, 0xffffffff)), "equal GUID words", failures)
	_expect_true(WowGuidScript.new(0, 0).is_empty(), "zero GUID sentinel", failures)
	_expect_true(not WowGuidScript.new(-1, 0).has_valid_word_ranges(), "negative GUID word", failures)
	_expect_true(not WowGuidScript.new(0x100000000, 0).has_valid_word_ranges(), "overflow GUID word", failures)


func _verify_server_entry_id(failures: Array[String]) -> void:
	var creature_entry = ServerEntryIdScript.new(" Creature_Template ", 123)
	var gameobject_entry = ServerEntryIdScript.new("gameobject_template", 123)
	_expect_true(creature_entry.is_valid(), "namespaced server entry", failures)
	_expect_equal(creature_entry.to_key(), "creature_template:123", "server entry key", failures)
	_expect_true(not creature_entry.equals(gameobject_entry), "cross-table entry collision prevented", failures)
	_expect_true(not ServerEntryIdScript.new("", 123).is_valid(), "missing entry namespace", failures)
	_expect_true(not ServerEntryIdScript.new("creature_template", 0).is_valid(), "zero server entry", failures)


func _verify_scene_free_sources(failures: Array[String]) -> void:
	for file_name in ["content_id.gd", "entity_id.gd", "wow_guid.gd", "server_entry_id.gd"]:
		var path := "res://src/domain/identity/%s" % file_name
		var file := FileAccess.open(path, FileAccess.READ)
		if file == null:
			failures.append("cannot open %s" % path)
			continue
		var source := file.get_as_text()
		_expect_true(not source.contains("extends Node"), "%s does not inherit Node" % file_name, failures)
		_expect_true(not source.contains("extends Resource"), "%s does not inherit Resource" % file_name, failures)
		_expect_true(not source.contains("Vector3"), "%s does not expose Vector3" % file_name, failures)


func _expect_equal(actual_value, expected_value, label: String, failures: Array[String]) -> void:
	if actual_value != expected_value:
		failures.append("%s expected %s, got %s" % [label, expected_value, actual_value])


func _expect_true(actual_value: bool, label: String, failures: Array[String]) -> void:
	if not actual_value:
		failures.append("%s expected true" % label)
