package clublog

import (
	"testing"
	"time"
)

func d(s string) time.Time {
	t, _ := time.Parse("2006-01-02", s)
	return t
}

// G1T is a contest call: Scotland ONLY from 2024-02-21; before that it's England
// by prefix. cty.dat's date-blind "=G1T → Scotland" override is wrong for a 2012
// QSO — the date-aware ClubLog cascade must give England then, Scotland now.
func TestG1TDatedException(t *testing.T) {
	db := &DB{
		exceptions: map[string][]Exception{
			"G1T": {{Call: "G1T", Entity: "Scotland", ADIF: 279, Start: d("2024-02-21")}},
		},
		prefixes: map[string][]Exception{
			"G": {{Entity: "England", ADIF: 223}},
		},
	}

	// 2012 QSO: exception doesn't cover it, but the call HAS an exception →
	// caller should use the prefix (England).
	if _, ok := db.Resolve("G1T", d("2012-07-29")); ok {
		t.Fatalf("2012: exception should NOT cover a pre-2024 date")
	}
	if !db.HasException("G1T") {
		t.Fatalf("HasException(G1T) should be true")
	}
	pe, pok := db.ResolvePrefix("G1T", d("2012-07-29"))
	if !pok || pe.Entity != "England" {
		t.Fatalf("2012 prefix: got %q ok=%v, want England", pe.Entity, pok)
	}

	// 2025 QSO: the exception covers it → Scotland.
	e, ok := db.Resolve("G1T", d("2025-01-01"))
	if !ok || e.Entity != "Scotland" {
		t.Fatalf("2025: got %q ok=%v, want Scotland", e.Entity, ok)
	}

	// An ordinary call with no exception → HasException false (caller leaves it
	// to cty.dat).
	if db.HasException("G4ABC") {
		t.Fatalf("HasException(G4ABC) should be false")
	}
}
