package award

import (
	"encoding/json"
	"testing"
)

// WAJA is numbered by the JARL, and the numbering is NOT Japan's ordinary
// prefecture code.
//
// The catalog shipped with the government's JIS numbering instead — 01
// Hokkaido, 02 Aomori, 03 Iwate, 04 Miyagi… — which agrees with the JARL's for
// the first three prefectures and then diverges for thirty-five of the
// remaining forty-four. The names were right throughout, so the award still
// counted the right contacts; every reference simply carried the wrong number,
// which is what an operator sends to the JARL when they claim it.
//
// The two schemes agree often enough to look correct at a glance, so this pins
// the places they differ rather than a count. Each pair below is one the old
// list got wrong, and the comment is what the old list said.
func TestCatalogWAJAUsesTheJARLNumbering(t *testing.T) {
	raw, ok := CatalogRefs("WAJA")
	if !ok {
		t.Fatal("WAJA has no reference list in the embedded catalog")
	}
	var refs []struct {
		Code    string `json:"code"`
		Name    string `json:"name"`
		Pattern string `json:"pattern"`
	}
	if err := json.Unmarshal(raw, &refs); err != nil {
		t.Fatalf("WAJA references: %v", err)
	}
	if len(refs) != 47 {
		t.Fatalf("WAJA has %d prefectures, want exactly 47", len(refs))
	}

	byName := map[string]string{}
	byCode := map[string]string{}
	for _, r := range refs {
		byName[r.Name] = r.Code
		if prev, dup := byCode[r.Code]; dup {
			t.Errorf("number %s is on both %s and %s", r.Code, prev, r.Name)
		}
		byCode[r.Code] = r.Name
	}

	for _, c := range []struct{ name, code string }{
		{"Hokkaido", "01"},  // the one both schemes agree on, and the anchor
		{"Miyagi", "06"},    // was 04
		{"Akita", "04"},     // was 05
		{"Niigata", "08"},   // was 15 — the JIS number
		{"Nagano", "09"},    // was 20
		{"Tokyo", "10"},     // was 13, the JIS number everyone recognises
		{"Kanagawa", "11"},  // was 14
		{"Saitama", "13"},   // was 11
		{"Ibaraki", "14"},   // was 8
		{"Gunma", "16"},     // was 10
		{"Yamanashi", "17"}, // was 19
		{"Kyoto", "22"},     // was 26
		{"Osaka", "25"},     // was 27
		{"Toyama", "28"},    // was 16
		{"Ishikawa", "30"},  // was 17
		{"Okayama", "31"},   // was 33
		{"Tottori", "34"},   // was 31
		{"Kagawa", "36"},    // was 37
		{"Tokushima", "37"}, // was 36
		{"Okinawa", "47"},   // unchanged: the far end of the list was already right
	} {
		if got := byName[c.name]; got != c.code {
			t.Errorf("%s is numbered %q, want %q on the JARL list", c.name, got, c.code)
		}
	}

	// Two digits throughout, as the JARL prints them. Not cosmetic: the codes
	// are strings, so "1" sorts between "09" and "10" and the panel showed the
	// prefectures in an order no list anywhere uses.
	for _, r := range refs {
		if len(r.Code) != 2 {
			t.Errorf("%s is numbered %q — the JARL list is two digits throughout", r.Name, r.Code)
		}
	}

	// The Tokyo spelling rule has to sit on Tokyo, and Tokyo moved. Left behind
	// on the old number it would be matching QTHs for Saitama.
	for _, r := range refs {
		if r.Pattern == "" {
			continue
		}
		if r.Name != "Tokyo" {
			t.Errorf("%s (%s) carries the pattern %q, which belongs to Tokyo", r.Name, r.Code, r.Pattern)
		}
	}
	if byName["Tokyo"] != "" {
		for _, r := range refs {
			if r.Name == "Tokyo" && r.Pattern == "" {
				t.Error("Tokyo lost its spelling pattern in the renumbering — Tokio would stop counting")
			}
		}
	}
}
