package iace

import "testing"

// TestGetComponentLibrary_EntryCount verifies the component library has at least 120 entries.
func TestGetComponentLibrary_EntryCount(t *testing.T) {
	entries := GetComponentLibrary()
	if len(entries) < 120 {
		t.Fatalf("GetComponentLibrary returned %d entries, want at least 120", len(entries))
	}
}

// TestGetComponentLibrary_UniqueIDs verifies all component IDs are unique.
func TestGetComponentLibrary_UniqueIDs(t *testing.T) {
	entries := GetComponentLibrary()
	seen := make(map[string]bool)
	for _, e := range entries {
		if e.ID == "" {
			t.Errorf("component has empty ID: %s", e.NameDE)
		}
		if seen[e.ID] {
			t.Errorf("duplicate component ID: %s", e.ID)
		}
		seen[e.ID] = true
	}
}

// TestGetComponentLibrary_ValidCategories verifies all categories are valid.
func TestGetComponentLibrary_ValidCategories(t *testing.T) {
	validCats := make(map[string]bool)
	for _, c := range ValidComponentLibraryCategories() {
		validCats[c] = true
	}
	for _, e := range GetComponentLibrary() {
		if !validCats[e.Category] {
			t.Errorf("component %s has invalid category %q", e.ID, e.Category)
		}
	}
}

// TestGetComponentLibrary_NonEmptyFields verifies required fields are filled.
func TestGetComponentLibrary_NonEmptyFields(t *testing.T) {
	for _, e := range GetComponentLibrary() {
		if e.NameDE == "" {
			t.Errorf("component %s: NameDE is empty", e.ID)
		}
		if e.NameEN == "" {
			t.Errorf("component %s: NameEN is empty", e.ID)
		}
		if e.MapsToComponentType == "" {
			t.Errorf("component %s: MapsToComponentType is empty", e.ID)
		}
		if len(e.Tags) == 0 {
			t.Errorf("component %s: Tags is empty", e.ID)
		}
	}
}

// TestGetComponentLibrary_CategoryDistribution verifies minimum category counts.
func TestGetComponentLibrary_CategoryDistribution(t *testing.T) {
	counts := make(map[string]int)
	for _, e := range GetComponentLibrary() {
		counts[e.Category]++
	}
	minimums := map[string]int{
		"mechanical":  10,
		"structural":  10,
		"drive":       10,
		"hydraulic":   10,
		"pneumatic":   10,
		"electrical":  10,
		"control":     10,
		"sensor":      10,
		"actuator":    10,
		"safety":      10,
		"it_network":  10,
	}
	for cat, minWant := range minimums {
		got := counts[cat]
		if got < minWant {
			t.Errorf("category %s: got %d entries, want at least %d", cat, got, minWant)
		}
	}
}

// TestGetEnergySources_EntryCount verifies the energy source library has exactly 20 entries.
func TestGetEnergySources_EntryCount(t *testing.T) {
	entries := GetEnergySources()
	if len(entries) != 20 {
		t.Fatalf("GetEnergySources returned %d entries, want 20", len(entries))
	}
}

// TestGetEnergySources_UniqueIDs verifies all energy source IDs are unique.
func TestGetEnergySources_UniqueIDs(t *testing.T) {
	entries := GetEnergySources()
	seen := make(map[string]bool)
	for _, e := range entries {
		if e.ID == "" {
			t.Errorf("energy source has empty ID: %s", e.NameDE)
		}
		if seen[e.ID] {
			t.Errorf("duplicate energy source ID: %s", e.ID)
		}
		seen[e.ID] = true
	}
}

// TestGetEnergySources_NonEmptyFields verifies required fields are filled.
func TestGetEnergySources_NonEmptyFields(t *testing.T) {
	for _, e := range GetEnergySources() {
		if e.NameDE == "" {
			t.Errorf("energy source %s: NameDE is empty", e.ID)
		}
		if e.NameEN == "" {
			t.Errorf("energy source %s: NameEN is empty", e.ID)
		}
		if len(e.Tags) == 0 {
			t.Errorf("energy source %s: Tags is empty", e.ID)
		}
	}
}

// TestGetEnergySources_AllHaveTags verifies every energy source has at least one tag.
func TestGetEnergySources_AllHaveTags(t *testing.T) {
	for _, e := range GetEnergySources() {
		if len(e.Tags) == 0 {
			t.Errorf("energy source %s has no tags", e.ID)
		}
	}
}
