package ucca

import "testing"

func TestKnowledgeSpaceOf(t *testing.T) {
	cases := map[string]string{
		"DSGVO":                "datenschutz",
		"BDSG":                 "datenschutz",
		"DSK SDM B51 ZUGRIFFE": "datenschutz",
		"EDPS DIGITAL ETHICS":  "datenschutz",
		"TRGS 900":             "arbeitsschutz",
		"OSHA 1910 SUBPART O":  "arbeitsschutz",
		"HGB":                  "wirtschaftsrecht",
		"BGB":                  "wirtschaftsrecht",
		"MASCHINENVO":          "produktsicherheit",
		"MVO":                  "produktsicherheit",
		"CRA":                  "cyber",
		"NIST SP800 53R5":      "cyber",
		"AI ACT":               "ki",
		"KI-VO":                "ki",
		"DORA":                 "finanz",
		"ARG":                  "arbeitsrecht",
		"":                     "",
	}
	for code, want := range cases {
		if got := KnowledgeSpaceOf(code); got != want {
			t.Errorf("KnowledgeSpaceOf(%q)=%q want %q", code, got, want)
		}
	}
}

func TestClassifyClarity(t *testing.T) {
	scattered := []LegalSearchResult{
		{RegulationCode: "CRA"}, {RegulationCode: "MASCHINENVO"}, {RegulationCode: "EU MDR"},
		{RegulationCode: "KI-VO"}, {RegulationCode: "TRBS 1111"}, {RegulationCode: "OWASP TOP10"},
	}
	if c := ClassifyClarity("Welche Risiken gibt es?", scattered); c.Mode != "clarify" {
		t.Errorf("scattered: mode=%q reason=%q want clarify", c.Mode, c.Reason)
	}
	concentrated := []LegalSearchResult{
		{RegulationCode: "DSGVO"}, {RegulationCode: "BDSG"}, {RegulationCode: "DSK SDM"},
		{RegulationCode: "EDPB WP243"}, {RegulationCode: "TDDDG"},
	}
	c := ClassifyClarity("Was ist eine DSFA?", concentrated)
	if c.Mode != "answer" || c.DominantContext != "datenschutz" {
		t.Errorf("concentrated: mode=%q dominant=%q want answer/datenschutz", c.Mode, c.DominantContext)
	}
}

func TestClassifyClarity_ExplicitScope(t *testing.T) {
	// G1: query names TRGS -> arbeitsschutz wins even though retrieval scatters to datenschutz.
	scattered := []LegalSearchResult{
		{RegulationCode: "DSK SDM METHODE"}, {RegulationCode: "DSK SDM V31"}, {RegulationCode: "DSK SDM B41 PLANEN"},
		{RegulationCode: "DSGVO"}, {RegulationCode: "DSK SDM"}, {RegulationCode: "TRGS 900"}, {RegulationCode: "TRGS 554"},
	}
	c := ClassifyClarity("Schwellwertanalyse nach TRGS", scattered)
	if c.Mode != "answer" || c.Reason != "explicit_scope" || c.DominantContext != "arbeitsschutz" {
		t.Errorf("explicit TRGS: mode=%q reason=%q dominant=%q want answer/explicit_scope/arbeitsschutz", c.Mode, c.Reason, c.DominantContext)
	}
	// no regulation named -> falls through to tiered logic
	if c := ClassifyClarity("Welche Risiken gibt es?", scattered); c.Reason == "explicit_scope" {
		t.Errorf("no reg named should not be explicit_scope, got %q", c.Reason)
	}
}
