package iace

import (
	"testing"
)

// expectedCrossRefCount must be updated as batches are added.
// Batches 1-6 × 100 + Batch 7 × 71 = 671 (full library coverage).
const expectedCrossRefCount = 671

func TestCrossRef_BatchCoverage(t *testing.T) {
	all := ListNormCrossRefs()
	if len(all) != expectedCrossRefCount {
		t.Fatalf("expected %d cross-ref entries, got %d", expectedCrossRefCount, len(all))
	}
}

func TestCrossRef_EN8120_HasASME(t *testing.T) {
	cr := GetNormCrossRef("EN-81-20")
	hasASME := false
	for _, m := range cr.Mappings {
		if m.Region == "US-ASME" {
			hasASME = true
			break
		}
	}
	if !hasASME {
		t.Error("EN-81-20 (lifts) should map to ASME A17.1 in US-ASME region")
	}
}

func TestCrossRef_EN13445_HasMultipleRegions(t *testing.T) {
	cr := GetNormCrossRef("EN-13445-1")
	if len(cr.Mappings) < 4 {
		t.Errorf("EN-13445-1 (pressure vessels) should have 4+ regional mappings, got %d", len(cr.Mappings))
	}
}

func TestCrossRef_ISO12100_HasAllRegions(t *testing.T) {
	cr := GetNormCrossRef("ISO-12100")
	if cr.NormID != "ISO-12100" {
		t.Fatalf("expected NormID ISO-12100, got %q", cr.NormID)
	}
	wantRegions := map[string]bool{
		"EU-DIN":  false,
		"US-ANSI": false,
		"CN-GB":   false,
		"JP-JIS":  false,
	}
	for _, m := range cr.Mappings {
		if _, ok := wantRegions[m.Region]; ok {
			wantRegions[m.Region] = true
		}
	}
	for region, found := range wantRegions {
		if !found {
			t.Errorf("ISO-12100 missing mapping for region %q", region)
		}
	}
}

func TestCrossRef_EN60204_HasNFPA79(t *testing.T) {
	cr := GetNormCrossRef("EN-60204-1")
	hasNFPA := false
	for _, m := range cr.Mappings {
		if m.Region == "US-NFPA" && m.Identifier != "" {
			hasNFPA = true
			break
		}
	}
	if !hasNFPA {
		t.Error("EN-60204-1 should map to NFPA 79 in US-NFPA region")
	}
}

func TestCrossRef_UnknownID_ReturnsEmpty(t *testing.T) {
	cr := GetNormCrossRef("ISO-NOT-IN-REGISTRY")
	if len(cr.Mappings) != 0 {
		t.Errorf("expected empty mappings for unknown ID, got %d", len(cr.Mappings))
	}
	if cr.NormID != "ISO-NOT-IN-REGISTRY" {
		t.Errorf("expected NormID preserved, got %q", cr.NormID)
	}
}

func TestCrossRef_AllEntries_HaveValidRelation(t *testing.T) {
	valid := map[string]bool{
		"identical": true, "equivalent": true, "partial": true,
		"supersedes": true, "superseded_by": true,
	}
	for _, cr := range ListNormCrossRefs() {
		for _, m := range cr.Mappings {
			if !valid[m.Relation] {
				t.Errorf("%s region %s: invalid relation %q", cr.NormID, m.Region, m.Relation)
			}
		}
	}
}

func TestCrossRef_AllEntries_HaveValidConfidence(t *testing.T) {
	valid := map[string]bool{
		"verified": true, "high": true, "medium": true, "low": true,
	}
	for _, cr := range ListNormCrossRefs() {
		for _, m := range cr.Mappings {
			if !valid[m.Confidence] {
				t.Errorf("%s region %s: invalid confidence %q", cr.NormID, m.Region, m.Confidence)
			}
		}
	}
}
