package ucca

import "testing"

func structPoint(reg, name, scope, article string) qdrantScrollPoint {
	return qdrantScrollPoint{Payload: map[string]interface{}{
		"regulation_short":   reg,
		"regulation_name_de": name,
		"chunk_scope":        scope,
		"article":            article,
	}}
}

func TestAggregateStructure_CountsDistinctPerScope(t *testing.T) {
	points := []qdrantScrollPoint{
		structPoint("CRA", "Cyber Resilience Act", "section", "13"),
		structPoint("CRA", "Cyber Resilience Act", "section", "13"), // duplicate article → still 1
		structPoint("CRA", "Cyber Resilience Act", "section", "14"),
		structPoint("CRA", "Cyber Resilience Act", "annex", "Anhang-I"),
		structPoint("CRA", "Cyber Resilience Act", "annex", "Anhang-VII"),
		structPoint("DORA", "", "section", "6"),  // first sighting has no name →
		structPoint("DORA", "", "section", "19"), // regulation_name falls back to short
		structPoint("DORA", "", "recital", ""),   // empty article → ignored for distinct
		structPoint("", "x", "section", "1"),     // missing regulation → skipped entirely
	}

	got := aggregateStructure(points)

	if len(got) != 2 {
		t.Fatalf("want 2 acts, got %d (%+v)", len(got), got)
	}
	// CRA has more articles → sorts first.
	cra := got[0]
	if cra.RegulationShort != "CRA" || cra.Articles != 2 || cra.Annexes != 2 || cra.Recitals != 0 || cra.Chunks != 5 {
		t.Errorf("CRA wrong: %+v", cra)
	}
	dora := got[1]
	if dora.RegulationShort != "DORA" || dora.Articles != 2 || dora.Chunks != 3 {
		t.Errorf("DORA wrong: %+v", dora)
	}
	if dora.RegulationName != "DORA" {
		t.Errorf("DORA name fallback failed: %q", dora.RegulationName)
	}
}

func TestAggregateStructure_Empty(t *testing.T) {
	if got := aggregateStructure(nil); len(got) != 0 {
		t.Errorf("want empty, got %+v", got)
	}
}
