package qslcard

import (
	"strings"
	"testing"
)

// goodTemplate builds a minimal valid template the failure cases mutate.
func goodTemplate() Template {
	return Template{
		Schema: SchemaVersion,
		Name:   "test",
		Card:   Card{W: 1654, H: 1063, DPI: 300, BleedPx: 35},
		Hero:   Hero{Photo: "img_a.jpg", Crop: Crop{W: 1654, H: 1063}},
		Elements: []Element{
			{Type: ElemCallsign, Text: "{profile.callsign}", Font: "Archivo Black",
				Size: 220, X: 60, Y: 80, StylePreset: "gel_gold"},
			{Type: ElemInfoLine, Text: "Loc. {profile.grid}", Font: "system-bold-sans",
				Size: 29, X: 64, Y: 420, StylePreset: "outlined_white",
				StyleParams: &StyleParams{OutlineColor: "#22364e", OutlineWidth: 5}},
			{Type: ElemCountry, Flag: "auto", Label: "auto", X: 64, Y: 940, Size: 30},
			{Type: ElemInsert, Photo: "img_b.jpg", X: 1180, Y: 70, W: 400, BorderPx: 14},
		},
		QSOBox: &QSOBox{Enabled: true, X: 64, Y: 660, W: 760, H: 220,
			BG: "#ffffff", BGOpacity: 0.88, Radius: 12,
			Title:  "Confirming QSO with {qso.callsign}",
			Fields: []string{"qso_date", "time_on", "band", "mode", "rst_sent"},
			Footer: "{qso.qsl_msg}"},
	}
}

func TestValidateGood(t *testing.T) {
	if err := Validate(goodTemplate(), nil); err != nil {
		t.Fatalf("valid template rejected: %v", err)
	}
}

func TestValidateRejects(t *testing.T) {
	tests := []struct {
		name    string
		mutate  func(*Template)
		wantErr string
	}{
		{"wrong schema", func(t *Template) { t.Schema = 99 }, "schema version"},
		{"no hero photo", func(t *Template) { t.Hero.Photo = "" }, "hero photo"},
		{"unknown preset", func(t *Template) { t.Elements[0].StylePreset = "neon_pink" }, "style_preset"},
		{"element off card", func(t *Template) { t.Elements[0].X = 99999 }, "outside the card"},
		{"insert overflows", func(t *Template) { t.Elements[3].W = 9000 }, "outside the card"},
		{"unknown namespace", func(t *Template) { t.Elements[0].Text = "{station.callsign}" }, "namespace"},
		{"unknown element type", func(t *Template) { t.Elements[0].Type = "sticker" }, "element type"},
		{"unknown qso field", func(t *Template) { t.QSOBox.Fields = []string{"tx_pwr"} }, "qso box field"},
		{"param not in preset", func(t *Template) {
			t.Elements[1].StyleParams.Gradient = []string{"#fff"} // outlined_white has no gradient
		}, "not accepted by preset"},
	}
	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			tmpl := goodTemplate()
			tc.mutate(&tmpl)
			err := Validate(tmpl, nil)
			if err == nil {
				t.Fatalf("expected error containing %q, got nil", tc.wantErr)
			}
			if !strings.Contains(err.Error(), tc.wantErr) {
				t.Fatalf("error %q does not contain %q", err, tc.wantErr)
			}
		})
	}
}

func TestValidateMissingPhoto(t *testing.T) {
	tmpl := goodTemplate()
	exists := func(name string) bool { return name == "img_a.jpg" }
	err := Validate(tmpl, exists)
	if err == nil || !strings.Contains(err.Error(), "img_b.jpg") {
		t.Fatalf("expected missing-photo error for img_b.jpg, got %v", err)
	}
}

func TestParseRoundTrip(t *testing.T) {
	src := goodTemplate()
	data, err := Encode(src)
	if err != nil {
		t.Fatalf("encode: %v", err)
	}
	got, err := Parse(data)
	if err != nil {
		t.Fatalf("parse: %v", err)
	}
	if err := Validate(got, nil); err != nil {
		t.Fatalf("round-tripped template invalid: %v", err)
	}
	if got.Name != src.Name || len(got.Elements) != len(src.Elements) {
		t.Fatalf("round trip lost data: %+v", got)
	}
}

func TestParseRejectsUnknownKeys(t *testing.T) {
	_, err := Parse([]byte(`{"schema":1,"name":"x","sparkles":true}`))
	if err == nil {
		t.Fatal("expected unknown-key error, got nil")
	}
}

func TestResolve(t *testing.T) {
	tmpl := goodTemplate()
	vars := map[string]string{
		"profile.callsign": "F4ABC", "profile.grid": "IN88",
		"qso.callsign": "XV9Q", "qso.qsl_msg": "TNX 73",
		"qso.qso_date": "2026-06-11", "qso.time_on": "14:02",
		"qso.band": "20m", "qso.mode": "SSB", "qso.rst_sent": "59",
	}
	m := Resolve(tmpl, vars, CountryInfo{Label: "France", FlagISO: "fr"})
	if m.Template.Elements[0].Text != "F4ABC" {
		t.Fatalf("callsign not resolved: %q", m.Template.Elements[0].Text)
	}
	if m.Template.Elements[2].Flag != "fr" || m.Template.Elements[2].Label != "France" {
		t.Fatalf("country not resolved: %+v", m.Template.Elements[2])
	}
	if m.Template.QSOBox.Title != "Confirming QSO with XV9Q" {
		t.Fatalf("qso box title not resolved: %q", m.Template.QSOBox.Title)
	}
	if m.QSOFields["band"] != "20m" || m.QSOFields["rst_sent"] != "59" {
		t.Fatalf("qso fields not filled: %v", m.QSOFields)
	}
	// Source template must be untouched (Resolve works on a copy).
	if tmpl.Elements[0].Text != "{profile.callsign}" {
		t.Fatal("Resolve mutated its input template")
	}
}
