package uls

import (
	"testing"

	"hamlog/internal/award"
)

// The eight counties Connecticut still has for every purpose except the Census.
var ctLegalCounties = map[string]bool{
	"Fairfield": true, "Hartford": true, "Litchfield": true, "Middlesex": true,
	"New Haven": true, "New London": true, "Tolland": true, "Windham": true,
}

// TestCTCountyOnlyLegalCounties makes sure no planning region ever creeps back
// into the table — a regenerated file that quietly picked up "Capitol Region"
// would put every Hartford-area station back out of reach of the award, which
// is exactly the failure this table exists to end.
func TestCTCountyOnlyLegalCounties(t *testing.T) {
	if len(ctCounty) < 400 {
		t.Fatalf("only %d Connecticut ZIPs — the table looks truncated", len(ctCounty))
	}
	for zip, county := range ctCounty {
		if !ctLegalCounties[county] {
			t.Errorf("ZIP %s → %q, which is not one of Connecticut's eight counties", zip, county)
		}
		if len(zip) != 5 {
			t.Errorf("ZIP %q is not five digits", zip)
		}
	}
}

// TestCTCountyMatchesAward closes the loop: every county in the table has to
// produce a key the USA-CA reference actually lists. A county the resolver can
// name but the award cannot match is worth no more than the planning region it
// replaced.
func TestCTCountyMatchesAward(t *testing.T) {
	want := map[string]bool{
		"CT/FAIRFIELD": true, "CT/HARTFORD": true, "CT/LITCHFIELD": true,
		"CT/MIDDLESEX": true, "CT/NEWHAVEN": true, "CT/NEWLONDON": true,
		"CT/TOLLAND": true, "CT/WINDHAM": true,
	}
	for zip, county := range ctCounty {
		if k := award.USCountyKey("CT", county); !want[k] {
			t.Errorf("ZIP %s → %q → key %q, not a USA-CA county", zip, county, k)
		}
	}
}

// TestCTCountyKnownZips pins a ZIP in each county against ground truth, so a
// regeneration that silently shifted the whole table is caught. 06419 is the
// case that started this: Killingworth is in Middlesex, and GeoNames calls it
// "Lower Connecticut River Valley".
func TestCTCountyKnownZips(t *testing.T) {
	for zip, want := range map[string]string{
		"06419": "Middlesex",  // Killingworth
		"06001": "Hartford",   // Avon
		"06106": "Hartford",   // Hartford
		"06510": "New Haven",  // New Haven
		"06880": "Fairfield",  // Westport
		"06340": "New London", // Groton
		"06226": "Windham",    // Willimantic
		"06238": "Tolland",    // Coventry
		"06759": "Litchfield", // Litchfield
	} {
		if got := ctCounty[zip]; got != want {
			t.Errorf("ctCounty[%s] = %q, want %q", zip, got, want)
		}
	}
}
