package iace

import "testing"

// TestGetTagTaxonomy_EntryCount verifies the taxonomy has entries.
func TestGetTagTaxonomy_EntryCount(t *testing.T) {
	tags := GetTagTaxonomy()
	if len(tags) < 80 {
		t.Fatalf("GetTagTaxonomy returned %d entries, want at least 80", len(tags))
	}
}

// TestGetTagTaxonomy_UniqueIDs verifies all tag IDs are unique.
func TestGetTagTaxonomy_UniqueIDs(t *testing.T) {
	tags := GetTagTaxonomy()
	seen := make(map[string]bool)
	for _, tag := range tags {
		if tag.ID == "" {
			t.Error("tag with empty ID found")
			continue
		}
		if seen[tag.ID] {
			t.Errorf("duplicate tag ID: %s", tag.ID)
		}
		seen[tag.ID] = true
	}
}

// TestGetTagTaxonomy_ValidDomains verifies all tags have valid domains.
func TestGetTagTaxonomy_ValidDomains(t *testing.T) {
	validDomains := make(map[string]bool)
	for _, d := range ValidTagDomains() {
		validDomains[d] = true
	}

	for _, tag := range GetTagTaxonomy() {
		if !validDomains[tag.Domain] {
			t.Errorf("tag %s has invalid domain %q", tag.ID, tag.Domain)
		}
	}
}

// TestGetTagTaxonomy_NonEmptyFields verifies required fields are filled.
func TestGetTagTaxonomy_NonEmptyFields(t *testing.T) {
	for _, tag := range GetTagTaxonomy() {
		if tag.DescriptionDE == "" {
			t.Errorf("tag %s: DescriptionDE is empty", tag.ID)
		}
		if tag.DescriptionEN == "" {
			t.Errorf("tag %s: DescriptionEN is empty", tag.ID)
		}
	}
}

// TestGetTagTaxonomy_DomainDistribution verifies each domain has entries.
func TestGetTagTaxonomy_DomainDistribution(t *testing.T) {
	counts := make(map[string]int)
	for _, tag := range GetTagTaxonomy() {
		counts[tag.Domain]++
	}

	expectedDomains := ValidTagDomains()
	for _, d := range expectedDomains {
		if counts[d] == 0 {
			t.Errorf("domain %q has no tags", d)
		}
	}
}

// TestValidTagDomains_HasFiveDomains verifies exactly 5 domains exist.
func TestValidTagDomains_HasFiveDomains(t *testing.T) {
	domains := ValidTagDomains()
	if len(domains) != 5 {
		t.Errorf("ValidTagDomains returned %d domains, want 5: %v", len(domains), domains)
	}
}

// TestGetTagTaxonomy_ComponentDomainHasMovingPart checks essential component tags.
func TestGetTagTaxonomy_ComponentDomainHasMovingPart(t *testing.T) {
	tagSet := make(map[string]string)
	for _, tag := range GetTagTaxonomy() {
		tagSet[tag.ID] = tag.Domain
	}

	essentialComponentTags := []string{
		"moving_part", "rotating_part", "high_voltage", "networked", "has_ai",
		"electrical_part", "sensor_part", "safety_device",
	}
	for _, id := range essentialComponentTags {
		domain, ok := tagSet[id]
		if !ok {
			t.Errorf("essential component tag %q not found in taxonomy", id)
		} else if domain != "component" {
			t.Errorf("tag %q expected domain 'component', got %q", id, domain)
		}
	}
}

// TestGetTagTaxonomy_EnergyDomainHasKinetic checks essential energy tags.
func TestGetTagTaxonomy_EnergyDomainHasKinetic(t *testing.T) {
	tagSet := make(map[string]string)
	for _, tag := range GetTagTaxonomy() {
		tagSet[tag.ID] = tag.Domain
	}

	essentialEnergyTags := []string{
		"kinetic", "electrical_energy", "hydraulic_pressure",
	}
	for _, id := range essentialEnergyTags {
		domain, ok := tagSet[id]
		if !ok {
			t.Errorf("essential energy tag %q not found in taxonomy", id)
		} else if domain != "energy" {
			t.Errorf("tag %q expected domain 'energy', got %q", id, domain)
		}
	}
}
