package services

import "testing"

// The variable catalogue is the single source of truth for the sidebar
// form + authoring palette labels (t-paliad-349 slice 5). These checks
// pin its integrity so a resolver Keys() edit can't silently ship a
// malformed entry or a duplicate key.
func TestSubmissionVariableCatalogue(t *testing.T) {
	cat := SubmissionVariableCatalogue()
	if len(cat) == 0 {
		t.Fatal("catalogue is empty")
	}

	seen := map[string]bool{}
	for _, e := range cat {
		if e.Key == "" || e.LabelDE == "" || e.LabelEN == "" || e.Group == "" {
			t.Errorf("incomplete catalogue entry: %+v", e)
		}
		if seen[e.Key] {
			t.Errorf("duplicate catalogue key: %q", e.Key)
		}
		seen[e.Key] = true
	}

	// Spot-check one key per namespace resolves with the expected label.
	want := map[string]struct{ group, de string }{
		"firm.name":                            {"firm", "Kanzlei"},
		"today.long_de":                        {"today", "Heute (DE lang)"},
		"user.display_name":                    {"user", "Bearbeiter"},
		"project.case_number":                  {"project", "Aktenzeichen (Gericht)"},
		"parties.claimant.name":                {"parties", "Klägerin"},
		"procedural_event.legal_source_pretty": {"procedural_event", "Rechtsgrundlage"},
		"deadline.due_date":                    {"deadline", "Frist (ISO)"},
	}
	byKey := map[string]struct{ group, de string }{}
	for _, e := range cat {
		byKey[e.Key] = struct{ group, de string }{e.Group, e.LabelDE}
	}
	for k, exp := range want {
		got, ok := byKey[k]
		if !ok {
			t.Errorf("catalogue missing expected key %q", k)
			continue
		}
		if got.group != exp.group || got.de != exp.de {
			t.Errorf("catalogue[%q] = {%q, %q}; want {%q, %q}", k, got.group, got.de, exp.group, exp.de)
		}
	}

	// The legacy rule.* aliases must be present for labelFor coverage.
	if !seen["rule.name"] || !seen["rule.legal_source_pretty"] {
		t.Error("legacy rule.* aliases missing from catalogue")
	}
}
