package main

import (
	"testing"

	"hamlog/internal/adif"
)

// What counts as a QRZ.com confirmation: app_qrzlog_status = C, and nothing else.
//
// Narrowed twice, each time on evidence from an operator's own log.
//
// 2026-07-29 — qsl_rcvd = Y was accepted. That is the operator's own PAPER QSL
// flag, which they uploaded to QRZ themselves, so every QSO with a card came
// back looking QRZ-confirmed.
//
// 2026-07-31 — qrzcom_qso_download_status = Y was accepted, and this test said
// it should be. A fetch showed both fields on ONE record:
//
//	<app_qrzlog_status:1>N  <qrzcom_qso_download_status:1>Y
//
// QRZ says not confirmed and sets the download field anyway: it marks what was
// handed back, not what was confirmed. Eighteen QSOs, all "UPDATED", all green,
// none of them confirmed on QRZ.
//
// This status feeds the award slots, so a false Y is a QSO counted as confirmed
// when it is not — the reason the rule is narrow rather than generous.
func TestQRZRecordConfirmed(t *testing.T) {
	cases := []struct {
		name string
		rec  adif.Record
		want bool
	}{
		// QRZ's own statement.
		{"QRZ log status C", adif.Record{"app_qrzlog_status": "C"}, true},
		{"lower case is still QRZ's answer", adif.Record{"app_qrzlog_status": "c"}, true},
		{"padded", adif.Record{"app_qrzlog_status": " C "}, true},

		// NOT confirmations by QRZ — the bug being fixed.
		{"paper QSL received", adif.Record{"qsl_rcvd": "Y"}, false},
		{"paper QSL plus a card sent", adif.Record{"qsl_rcvd": "Y", "qsl_sent": "Y"}, false},
		{"LoTW confirmed, QRZ silent", adif.Record{"lotw_qsl_rcvd": "Y"}, false},
		{"eQSL confirmed, QRZ silent", adif.Record{"eqsl_qsl_rcvd": "Y"}, false},

		// The download flag is not an answer: QRZ sets it on everything it returns.
		{"downloaded, not confirmed", adif.Record{"qrzcom_qso_download_status": "Y"}, false},
		{"the record from the report", adif.Record{
			"call": "IQ4J", "band": "17m", "mode": "FT8",
			"qrzcom_qso_upload_status": "Y", "app_qrzlog_status": "N",
			"qrzcom_qso_download_date": "20260731", "qrzcom_qso_download_status": "Y",
		}, false},

		// Explicit negatives and nothing at all.
		{"QRZ says no", adif.Record{"qrzcom_qso_download_status": "N"}, false},
		{"QRZ status not confirmed", adif.Record{"app_qrzlog_status": "N"}, false},
		{"bare record", adif.Record{"call": "F4XYZ"}, false},
		{"empty", adif.Record{}, false},
	}
	for _, c := range cases {
		if got := qrzRecordConfirmed(c.rec); got != c.want {
			t.Errorf("%s: qrzRecordConfirmed = %v, want %v (%v)", c.name, got, c.want, c.rec)
		}
	}
}
