package main

import (
	"testing"

	"hamlog/internal/qso"
)

// The confirmation statuses an import fills in, and the ones it must not touch.
//
// An operator asked for this feature believing it missing — it has been there
// since v0.14, but the option's description never mentioned it, so nobody could
// know. The behaviour is pinned here so a future edit cannot quietly drop what
// the description now promises.
//
// The rule is fill-if-empty: a WSJT-X log carries almost no QSL fields, while a
// log exported from another program carries real ones that must survive.
func TestQSLDefaultsFillOnlyEmptyFields(t *testing.T) {
	defaults := QSLDefaults{
		QSLSent: "N", QSLRcvd: "N",
		LOTWSent: "N", LOTWRcvd: "N",
		EQSLSent: "N", EQSLRcvd: "N",
		ClublogStatus: "N", HRDLogStatus: "N",
		QRZComStatus: "N", QRZComCfm: "N",
	}

	// A record as WSJT-X writes it: no confirmation fields at all.
	bare := &qso.QSO{Callsign: "F4XYZ"}
	applyQSLDefaultsTo(bare, defaults)
	for name, got := range map[string]string{
		"qsl_sent": bare.QSLSent, "qsl_rcvd": bare.QSLRcvd,
		"lotw_sent": bare.LOTWSent, "lotw_rcvd": bare.LOTWRcvd,
		"eqsl_sent": bare.EQSLSent, "eqsl_rcvd": bare.EQSLRcvd,
		"clublog": bare.ClublogUploadStatus, "hrdlog": bare.HRDLogUploadStatus,
		"qrz_sent": bare.QRZComUploadStatus, "qrz_rcvd": bare.QRZComDownloadStatus,
	} {
		if got != "N" {
			t.Errorf("%s = %q on an empty record, want the default \"N\"", name, got)
		}
	}

	// A record that already carries confirmations — from a real logbook export.
	// Overwriting these would erase QSLs the operator has actually received.
	kept := &qso.QSO{
		Callsign: "F4XYZ",
		QSLRcvd:  "Y", LOTWRcvd: "Y", EQSLSent: "Y",
		ClublogUploadStatus: "Y", QRZComDownloadStatus: "Y",
	}
	applyQSLDefaultsTo(kept, defaults)
	for name, got := range map[string]string{
		"qsl_rcvd": kept.QSLRcvd, "lotw_rcvd": kept.LOTWRcvd, "eqsl_sent": kept.EQSLSent,
		"clublog": kept.ClublogUploadStatus, "qrz_rcvd": kept.QRZComDownloadStatus,
	} {
		if got != "Y" {
			t.Errorf("%s = %q — an existing confirmation was overwritten", name, got)
		}
	}
	// …while its empty ones still get the default.
	if kept.QSLSent != "N" || kept.LOTWSent != "N" {
		t.Errorf("empty fields on a partly-filled record were not defaulted: qsl_sent=%q lotw_sent=%q",
			kept.QSLSent, kept.LOTWSent)
	}
}
