package lookup

import "testing"

func TestHomeCall(t *testing.T) {
	cases := map[string]string{
		"JW/OR1A":   "OR1A",  // portable prefix → home call
		"DL/F4NIE":  "F4NIE", // prefix/home
		"F4BPO/P":   "F4BPO", // suffix dropped
		"F4BPO/9":   "F4BPO", // call-area digit dropped
		"VP8/F4BPO": "F4BPO", // both have digits → longest wins
		"MM/KA9P":   "KA9P",  // Scotland prefix + US home
		"OH2BH":     "OH2BH", // no slash → unchanged
		"3D2/RW3RN": "RW3RN",
	}
	for in, want := range cases {
		if got := homeCall(in); got != want {
			t.Errorf("homeCall(%q) = %q, want %q", in, got, want)
		}
	}
}

// Mobile and maritime-mobile suffixes: /M is the case that surfaced in the field
// (F4LYI/M resolved only to cty.dat while F4LYI resolved on QRZ), so pin the
// whole suffix family — the home call is what the provider record is filed under.
func TestHomeCallSuffixes(t *testing.T) {
	for call, want := range map[string]string{
		"F4LYI/M":   "F4LYI",
		"F4LYI/MM":  "F4LYI",
		"F4LYI/AM":  "F4LYI",
		"F4LYI/QRP": "F4LYI",
		"F4LYI/A":   "F4LYI",
		"F4LYI/B":   "F4LYI",
	} {
		if got := homeCall(call); got != want {
			t.Errorf("homeCall(%q) = %q, want %q", call, got, want)
		}
	}
}

// Operational suffixes must resolve to the bare call WITHOUT a provider query on
// the slashed form; entity- and area-changing forms must not be stripped.
func TestStripOpSuffix(t *testing.T) {
	strip := map[string]string{
		"F4LYI/M": "F4LYI", "F4LYI/MM": "F4LYI", "F4LYI/AM": "F4LYI",
		"F4LYI/P": "F4LYI", "F4LYI/QRP": "F4LYI", "F4LYI/p": "F4LYI",
		"F4BPO/M/P": "F4BPO",
	}
	for call, want := range strip {
		got, ok := stripOpSuffix(call)
		if !ok || got != want {
			t.Errorf("stripOpSuffix(%q) = %q,%v — want %q,true", call, got, ok, want)
		}
	}
	// These change the entity or the call area: they are real, separately
	// registered forms and must be queried exactly as entered.
	for _, call := range []string{"JW/OR1A", "VP8/F4BPO", "F4BPO/8", "DL/F4NIE", "OH2BH", "F4BPO/W6"} {
		if got, ok := stripOpSuffix(call); ok {
			t.Errorf("stripOpSuffix(%q) stripped to %q — it changes entity/area and must be kept", call, got)
		}
	}
}
