package iace

import "testing"

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

// TestGetBuiltinHazardPatterns_Count verifies the expected number of patterns.
func TestGetBuiltinHazardPatterns_Count(t *testing.T) {
	patterns := GetBuiltinHazardPatterns()
	if len(patterns) < 40 {
		t.Fatalf("expected at least 40 patterns, got %d", len(patterns))
	}
}

// TestGetBuiltinHazardPatterns_AllHaveRequiredTags verifies every pattern has at
// least one required component tag or energy tag.
func TestGetBuiltinHazardPatterns_AllHaveRequiredTags(t *testing.T) {
	for _, p := range GetBuiltinHazardPatterns() {
		if len(p.RequiredComponentTags) == 0 && len(p.RequiredEnergyTags) == 0 {
			t.Errorf("pattern %s (%s) has no required component or energy tags", p.ID, p.NameDE)
		}
	}
}

// TestGetBuiltinHazardPatterns_AllHaveGeneratedHazardCats verifies every pattern
// generates at least one hazard category.
func TestGetBuiltinHazardPatterns_AllHaveGeneratedHazardCats(t *testing.T) {
	for _, p := range GetBuiltinHazardPatterns() {
		if len(p.GeneratedHazardCats) == 0 {
			t.Errorf("pattern %s (%s) has no generated hazard categories", p.ID, p.NameDE)
		}
	}
}

// TestGetBuiltinHazardPatterns_AllHaveSuggestedMeasures verifies every pattern
// suggests at least one measure.
func TestGetBuiltinHazardPatterns_AllHaveSuggestedMeasures(t *testing.T) {
	for _, p := range GetBuiltinHazardPatterns() {
		if len(p.SuggestedMeasureIDs) == 0 {
			t.Errorf("pattern %s (%s) has no suggested measure IDs", p.ID, p.NameDE)
		}
	}
}

// TestGetBuiltinHazardPatterns_AllHaveSuggestedEvidence verifies every pattern
// suggests at least one evidence type.
func TestGetBuiltinHazardPatterns_AllHaveSuggestedEvidence(t *testing.T) {
	for _, p := range GetBuiltinHazardPatterns() {
		if len(p.SuggestedEvidenceIDs) == 0 {
			t.Errorf("pattern %s (%s) has no suggested evidence IDs", p.ID, p.NameDE)
		}
	}
}

// TestGetBuiltinHazardPatterns_ValidPriorities verifies all priorities are 1-100.
func TestGetBuiltinHazardPatterns_ValidPriorities(t *testing.T) {
	for _, p := range GetBuiltinHazardPatterns() {
		if p.Priority < 1 || p.Priority > 100 {
			t.Errorf("pattern %s has invalid priority %d (want 1-100)", p.ID, p.Priority)
		}
	}
}

// TestGetBuiltinHazardPatterns_ReferencedMeasuresExist verifies all referenced
// measure IDs exist in the protective measures library.
func TestGetBuiltinHazardPatterns_ReferencedMeasuresExist(t *testing.T) {
	measureIDs := make(map[string]bool)
	for _, m := range GetProtectiveMeasureLibrary() {
		measureIDs[m.ID] = true
	}

	for _, p := range GetBuiltinHazardPatterns() {
		for _, mid := range p.SuggestedMeasureIDs {
			if !measureIDs[mid] {
				t.Errorf("pattern %s references non-existent measure ID %s", p.ID, mid)
			}
		}
	}
}

// TestGetBuiltinHazardPatterns_ReferencedEvidenceExist verifies all referenced
// evidence IDs exist in the evidence type library.
func TestGetBuiltinHazardPatterns_ReferencedEvidenceExist(t *testing.T) {
	evidenceIDs := make(map[string]bool)
	for _, e := range GetEvidenceTypeLibrary() {
		evidenceIDs[e.ID] = true
	}

	for _, p := range GetBuiltinHazardPatterns() {
		for _, eid := range p.SuggestedEvidenceIDs {
			if !evidenceIDs[eid] {
				t.Errorf("pattern %s references non-existent evidence ID %s", p.ID, eid)
			}
		}
	}
}

// TestGetBuiltinHazardPatterns_HazardCategoriesValid verifies all generated
// hazard categories match known categories in the hazard library.
func TestGetBuiltinHazardPatterns_HazardCategoriesValid(t *testing.T) {
	validCategories := make(map[string]bool)
	for _, h := range GetBuiltinHazardLibrary() {
		validCategories[h.Category] = true
	}
	// ISO 12100 Annex B splits Nr. 4 Laerm and Nr. 5 Vibration into two
	// top-level groups. Patterns adopted this in 2026-05 but the hazard
	// library still carries entries under the legacy combined "noise_-
	// vibration" key. Until the library is split, both new aliases are
	// recognised as valid pattern categories.
	validCategories["noise_hazard"] = true
	validCategories["vibration_hazard"] = true

	for _, p := range GetBuiltinHazardPatterns() {
		for _, cat := range p.GeneratedHazardCats {
			if !validCategories[cat] {
				t.Errorf("pattern %s references unknown hazard category %q", p.ID, cat)
			}
		}
	}
}
