package awardref

import (
	"strconv"

	"hamlog/internal/dxcc"
)

// BuiltinRefs returns the seed reference list for a built-in award (DXCC
// entities, CQ zones, continents, US states, French departments). ok=false for
// awards whose list is downloaded online (POTA/SOTA/WWFF) or fully custom.
//
// The reference CODE must equal what award.Compute extracts from the QSO field
// so worked references map onto the list:
//   - DXCC  → entity number ("291")
//   - WAZ   → CQ zone number ("1".."40")
//   - WAC   → continent code ("EU", "NA", …)
//   - WAS   → ADIF STATE code ("AL", …)
//   - DDFM  → "D06" (the award pattern captures the leading D)
//   - USA-CA → canonical "STATE/COUNTY" key (see award.USCountyKey)
func BuiltinRefs(code string) ([]Ref, bool) {
	switch code {
	case "DXCC":
		return dxccEntities(), true
	case "WAZ":
		return cqZones(), true
	case "WAC":
		return continents(), true
	case "WAS":
		return usStates().Refs, true
	case "DDFM":
		return frenchDepartments(), true
	case "USA-CA":
		return usCounties(), true
	}
	return nil, false
}

func dxccEntities() []Ref {
	ents := dxcc.AllEntities()
	out := make([]Ref, 0, len(ents))
	for _, e := range ents {
		out = append(out, ref(strconv.Itoa(e.Num), e.Name, e.Num))
	}
	return out
}

func cqZones() []Ref {
	out := make([]Ref, 0, 40)
	for z := 1; z <= 40; z++ {
		out = append(out, ref(strconv.Itoa(z), "CQ Zone "+strconv.Itoa(z), 0))
	}
	return out
}

func continents() []Ref {
	pairs := [][2]string{
		{"AF", "Africa"}, {"AN", "Antarctica"}, {"AS", "Asia"},
		{"EU", "Europe"}, {"NA", "North America"}, {"OC", "Oceania"}, {"SA", "South America"},
	}
	out := make([]Ref, 0, len(pairs))
	for _, p := range pairs {
		out = append(out, ref(p[0], p[1], 0))
	}
	return out
}

// frenchDepartments — the 96 metropolitan French departments (DXCC 227).
func frenchDepartments() []Ref {
	const fr = 227
	deps := [][2]string{
		{"D01", "Ain"}, {"D02", "Aisne"}, {"D03", "Allier"}, {"D04", "Alpes-de-Haute-Provence"},
		{"D05", "Hautes-Alpes"}, {"D06", "Alpes-Maritimes"}, {"D07", "Ardèche"}, {"D08", "Ardennes"},
		{"D09", "Ariège"}, {"D10", "Aube"}, {"D11", "Aude"}, {"D12", "Aveyron"},
		{"D13", "Bouches-du-Rhône"}, {"D14", "Calvados"}, {"D15", "Cantal"}, {"D16", "Charente"},
		{"D17", "Charente-Maritime"}, {"D18", "Cher"}, {"D19", "Corrèze"}, {"D2A", "Corse-du-Sud"},
		{"D2B", "Haute-Corse"}, {"D21", "Côte-d'Or"}, {"D22", "Côtes-d'Armor"}, {"D23", "Creuse"},
		{"D24", "Dordogne"}, {"D25", "Doubs"}, {"D26", "Drôme"}, {"D27", "Eure"},
		{"D28", "Eure-et-Loir"}, {"D29", "Finistère"}, {"D30", "Gard"}, {"D31", "Haute-Garonne"},
		{"D32", "Gers"}, {"D33", "Gironde"}, {"D34", "Hérault"}, {"D35", "Ille-et-Vilaine"},
		{"D36", "Indre"}, {"D37", "Indre-et-Loire"}, {"D38", "Isère"}, {"D39", "Jura"},
		{"D40", "Landes"}, {"D41", "Loir-et-Cher"}, {"D42", "Loire"}, {"D43", "Haute-Loire"},
		{"D44", "Loire-Atlantique"}, {"D45", "Loiret"}, {"D46", "Lot"}, {"D47", "Lot-et-Garonne"},
		{"D48", "Lozère"}, {"D49", "Maine-et-Loire"}, {"D50", "Manche"}, {"D51", "Marne"},
		{"D52", "Haute-Marne"}, {"D53", "Mayenne"}, {"D54", "Meurthe-et-Moselle"}, {"D55", "Meuse"},
		{"D56", "Morbihan"}, {"D57", "Moselle"}, {"D58", "Nièvre"}, {"D59", "Nord"},
		{"D60", "Oise"}, {"D61", "Orne"}, {"D62", "Pas-de-Calais"}, {"D63", "Puy-de-Dôme"},
		{"D64", "Pyrénées-Atlantiques"}, {"D65", "Hautes-Pyrénées"}, {"D66", "Pyrénées-Orientales"}, {"D67", "Bas-Rhin"},
		{"D68", "Haut-Rhin"}, {"D69", "Rhône"}, {"D70", "Haute-Saône"}, {"D71", "Saône-et-Loire"},
		{"D72", "Sarthe"}, {"D73", "Savoie"}, {"D74", "Haute-Savoie"}, {"D75", "Paris"},
		{"D76", "Seine-Maritime"}, {"D77", "Seine-et-Marne"}, {"D78", "Yvelines"}, {"D79", "Deux-Sèvres"},
		{"D80", "Somme"}, {"D81", "Tarn"}, {"D82", "Tarn-et-Garonne"}, {"D83", "Var"},
		{"D84", "Vaucluse"}, {"D85", "Vendée"}, {"D86", "Vienne"}, {"D87", "Haute-Vienne"},
		{"D88", "Vosges"}, {"D89", "Yonne"}, {"D90", "Territoire de Belfort"}, {"D91", "Essonne"},
		{"D92", "Hauts-de-Seine"}, {"D93", "Seine-Saint-Denis"}, {"D94", "Val-de-Marne"}, {"D95", "Val-d'Oise"},
	}
	out := make([]Ref, 0, len(deps))
	for _, d := range deps {
		out = append(out, ref(d[0], d[1], fr))
	}
	return out
}
