package adif

import (
	"bufio"
	"context"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
	"time"

	"hamlog/internal/qso"
)

// ExportResult summarises an ADIF export for the UI.
type ExportResult struct {
	Path    string `json:"path"`
	Count   int    `json:"count"`
	SizeKB  int64  `json:"size_kb"`
}

// Exporter streams every QSO in a repo to an ADIF (.adi) file.
type Exporter struct {
	Repo *qso.Repo

	// AppName / AppVersion populate the ADIF header comments. Optional.
	AppName    string
	AppVersion string

	// IncludeAppFields controls whether application-specific fields (ADIF
	// "APP_<programid>_<name>" tags, e.g. Log4OM's APP_LOG4OM_* or our own
	// OpsLog extensions) are written. Leave false for a clean standard-ADIF
	// export destined for another logger; set true for a full OpsLog→OpsLog
	// round-trip that preserves everything.
	IncludeAppFields bool
}

// iterator streams QSOs through fn. The three concrete sources (all, filtered,
// by-ids) all match this shape so the document writer stays source-agnostic.
type iterator func(ctx context.Context, fn func(qso.QSO) error) error

// ExportFile creates path (overwriting if it exists) and writes every QSO.
func (e *Exporter) ExportFile(ctx context.Context, path string) (ExportResult, error) {
	return e.exportFileWith(ctx, path, e.Repo.IterateAll)
}

// ExportFileFiltered writes only the QSOs matching f (no row limit).
func (e *Exporter) ExportFileFiltered(ctx context.Context, path string, f qso.QueryFilter) (ExportResult, error) {
	return e.exportFileWith(ctx, path, func(ctx context.Context, fn func(qso.QSO) error) error {
		return e.Repo.IterateFiltered(ctx, f, fn)
	})
}

// ExportFileByIDs writes only the QSOs with the given ids.
func (e *Exporter) ExportFileByIDs(ctx context.Context, path string, ids []int64) (ExportResult, error) {
	return e.exportFileWith(ctx, path, func(ctx context.Context, fn func(qso.QSO) error) error {
		return e.Repo.IterateByIDs(ctx, ids, fn)
	})
}

func (e *Exporter) exportFileWith(ctx context.Context, path string, iter iterator) (ExportResult, error) {
	f, err := os.Create(path)
	if err != nil {
		return ExportResult{}, fmt.Errorf("create %s: %w", path, err)
	}
	defer f.Close()

	count, err := e.writeDoc(ctx, f, iter)
	if err != nil {
		return ExportResult{Path: path, Count: count}, err
	}
	info, _ := f.Stat()
	return ExportResult{Path: path, Count: count, SizeKB: info.Size() / 1024}, nil
}

// Export writes a complete ADIF document (header + records + EOF) to w for
// every QSO. Returns the number of QSOs written.
func (e *Exporter) Export(ctx context.Context, w io.Writer) (int, error) {
	return e.writeDoc(ctx, w, e.Repo.IterateAll)
}

// writeDoc writes the ADIF header then streams every QSO from iter.
func (e *Exporter) writeDoc(ctx context.Context, w io.Writer, iter iterator) (int, error) {
	bw := bufio.NewWriterSize(w, 64*1024)
	defer bw.Flush()

	app := strings.TrimSpace(e.AppName)
	if app == "" {
		app = "HamLog"
	}
	ver := strings.TrimSpace(e.AppVersion)
	now := time.Now().UTC().Format("20060102 150405")
	fmt.Fprintf(bw, "# ADIF export by %s %s — %s UTC\n", app, ver, now)
	fmt.Fprintf(bw, "<ADIF_VER:%d>%s <PROGRAMID:%d>%s", len(adifVersion), adifVersion, len(app), app)
	if ver != "" {
		fmt.Fprintf(bw, " <PROGRAMVERSION:%d>%s", len(ver), ver)
	}
	fmt.Fprintf(bw, " <CREATED_TIMESTAMP:15>%s <EOH>\n\n", now)

	count := 0
	err := iter(ctx, func(q qso.QSO) error {
		writeRecord(bw, q, e.IncludeAppFields)
		count++
		return nil
	})
	return count, err
}

// SingleRecordADIF returns one QSO serialised as an ADIF record (fields
// terminated by <EOR>), with no document header. Used by the external-
// service uploaders (QRZ.com / Clublog / …) which want a bare record as
// the ADIF parameter of their HTTP API.
func SingleRecordADIF(q qso.QSO) string {
	var b strings.Builder
	bw := bufio.NewWriter(&b)
	// Uploads target other services — keep it standard (no app-specific tags).
	writeRecord(bw, q, false)
	bw.Flush()
	return b.String()
}

// FullRecordADIF serialises one QSO LOSSLESSLY — including the APP_* extras —
// so it can be written out and read back with nothing dropped. Used by the
// offline queue: a QSO parked in the safety file must come back identical
// (extras carry its queue id, awards refs, ADIF 3.1.7 leftovers…).
func FullRecordADIF(q qso.QSO) string {
	var b strings.Builder
	bw := bufio.NewWriter(&b)
	writeRecord(bw, q, true)
	bw.Flush()
	return b.String()
}

// BatchRecordsADIF wraps already-serialised records (each terminated by <EOR>,
// e.g. from SingleRecordADIF) in a minimal ADIF document with a standard
// header. Used by file-based batch upload APIs such as Club Log's putlogs.php.
func BatchRecordsADIF(records []string) string {
	var b strings.Builder
	now := time.Now().UTC().Format("20060102 150405")
	fmt.Fprintf(&b, "<ADIF_VER:%d>%s <PROGRAMID:6>OpsLog <CREATED_TIMESTAMP:15>%s <EOH>\n\n",
		len(adifVersion), adifVersion, now)
	for _, r := range records {
		r = strings.TrimRight(r, "\r\n")
		if r == "" {
			continue
		}
		b.WriteString(r)
		b.WriteString("\n")
	}
	return b.String()
}

// writeRecord serialises one QSO as ADIF tags terminated by <EOR>.
// Empty fields are omitted. MODE/SUBMODE are massaged so a "promoted"
// mode (e.g. FT4 stored without a parent) is exported as the canonical
// pair MODE=MFSK SUBMODE=FT4 — round-trips cleanly with strict loggers.
func writeRecord(bw *bufio.Writer, q qso.QSO, includeApp bool) {
	// --- Core ---
	writeField(bw, "CALL", q.Callsign)

	if !q.QSODate.IsZero() {
		writeField(bw, "QSO_DATE", q.QSODate.UTC().Format("20060102"))
		writeField(bw, "TIME_ON", q.QSODate.UTC().Format("150405"))
	}
	if !q.QSODateOff.IsZero() {
		writeField(bw, "QSO_DATE_OFF", q.QSODateOff.UTC().Format("20060102"))
		writeField(bw, "TIME_OFF", q.QSODateOff.UTC().Format("150405"))
	}
	writeField(bw, "BAND", q.Band)
	writeField(bw, "BAND_RX", q.BandRX)

	mode, submode := modeForExport(q.Mode, q.Submode)
	writeField(bw, "MODE", mode)
	writeField(bw, "SUBMODE", submode)

	if q.FreqHz != nil && *q.FreqHz > 0 {
		writeField(bw, "FREQ", strconv.FormatFloat(float64(*q.FreqHz)/1_000_000, 'f', 6, 64))
	}
	if q.FreqRXHz != nil && *q.FreqRXHz > 0 {
		writeField(bw, "FREQ_RX", strconv.FormatFloat(float64(*q.FreqRXHz)/1_000_000, 'f', 6, 64))
	}

	writeField(bw, "RST_SENT", q.RSTSent)
	writeField(bw, "RST_RCVD", q.RSTRcvd)

	// --- Contacted ---
	writeField(bw, "NAME", q.Name)
	writeField(bw, "QTH", q.QTH)
	writeField(bw, "ADDRESS", q.Address)
	writeField(bw, "EMAIL", q.Email)
	writeField(bw, "WEB", q.Web)
	writeField(bw, "GRIDSQUARE", q.Grid)
	writeField(bw, "GRIDSQUARE_EXT", q.GridExt)
	writeField(bw, "VUCC_GRIDS", q.VUCCGrids)
	writeField(bw, "COUNTRY", q.Country)
	writeField(bw, "STATE", q.State)
	writeField(bw, "CNTY", q.County)
	writeIntPtr(bw, "DXCC", q.DXCC)
	writeField(bw, "CONT", q.Continent)
	writeIntPtr(bw, "CQZ", q.CQZ)
	writeIntPtr(bw, "ITUZ", q.ITUZ)
	writeField(bw, "IOTA", q.IOTA)
	writeField(bw, "SOTA_REF", q.SOTARef)
	writeField(bw, "POTA_REF", q.POTARef)
	writeIntPtr(bw, "AGE", q.Age)
	writeFloatPtr(bw, "LAT", q.Lat, 6)
	writeFloatPtr(bw, "LON", q.Lon, 6)
	writeField(bw, "RIG", q.Rig)
	writeField(bw, "ANT", q.Ant)

	// --- QSL / LoTW / eQSL / Clublog / HRDLog ---
	writeField(bw, "QSL_SENT", q.QSLSent)
	writeField(bw, "QSL_RCVD", q.QSLRcvd)
	writeField(bw, "QSLSDATE", q.QSLSentDate)
	writeField(bw, "QSLRDATE", q.QSLRcvdDate)
	writeField(bw, "QSL_VIA", q.QSLVia)
	writeField(bw, "QSLMSG", q.QSLMsg)
	writeField(bw, "QSLMSG_RCVD", q.QSLMsgRcvd)
	writeField(bw, "LOTW_QSL_SENT", q.LOTWSent)
	writeField(bw, "LOTW_QSL_RCVD", q.LOTWRcvd)
	writeField(bw, "LOTW_QSLSDATE", q.LOTWSentDate)
	writeField(bw, "LOTW_QSLRDATE", q.LOTWRcvdDate)
	writeField(bw, "EQSL_QSL_SENT", q.EQSLSent)
	writeField(bw, "EQSL_QSL_RCVD", q.EQSLRcvd)
	writeField(bw, "EQSL_QSLSDATE", q.EQSLSentDate)
	writeField(bw, "EQSL_QSLRDATE", q.EQSLRcvdDate)
	writeField(bw, "CLUBLOG_QSO_UPLOAD_DATE", q.ClublogUploadDate)
	writeField(bw, "CLUBLOG_QSO_UPLOAD_STATUS", q.ClublogUploadStatus)
	writeField(bw, "HRDLOG_QSO_UPLOAD_DATE", q.HRDLogUploadDate)
	writeField(bw, "HRDLOG_QSO_UPLOAD_STATUS", q.HRDLogUploadStatus)
	writeField(bw, "QRZCOM_QSO_UPLOAD_DATE", q.QRZComUploadDate)
	writeField(bw, "QRZCOM_QSO_UPLOAD_STATUS", q.QRZComUploadStatus)
	writeField(bw, "QRZCOM_QSO_DOWNLOAD_DATE", q.QRZComDownloadDate)
	writeField(bw, "QRZCOM_QSO_DOWNLOAD_STATUS", q.QRZComDownloadStatus)

	// --- Contest ---
	writeField(bw, "CONTEST_ID", q.ContestID)
	writeIntPtr(bw, "SRX", q.SRX)
	writeIntPtr(bw, "STX", q.STX)
	writeField(bw, "SRX_STRING", q.SRXString)
	writeField(bw, "STX_STRING", q.STXString)
	writeField(bw, "CHECK", q.Check)
	writeField(bw, "PRECEDENCE", q.Precedence)
	writeField(bw, "ARRL_SECT", q.ARRLSect)

	// --- Satellite / propagation ---
	writeField(bw, "PROP_MODE", q.PropMode)
	writeField(bw, "SAT_NAME", q.SatName)
	writeField(bw, "SAT_MODE", q.SatMode)
	writeFloatPtr(bw, "ANT_AZ", q.AntAz, 1)
	writeFloatPtr(bw, "ANT_EL", q.AntEl, 1)
	writeField(bw, "ANT_PATH", q.AntPath)

	// --- My station / operator ---
	writeField(bw, "STATION_CALLSIGN", q.StationCallsign)
	writeField(bw, "OPERATOR", q.Operator)
	writeField(bw, "MY_GRIDSQUARE", q.MyGrid)
	writeField(bw, "MY_GRIDSQUARE_EXT", q.MyGridExt)
	writeField(bw, "MY_COUNTRY", q.MyCountry)
	writeField(bw, "MY_STATE", q.MyState)
	writeField(bw, "MY_CNTY", q.MyCounty)
	writeField(bw, "MY_IOTA", q.MyIOTA)
	writeField(bw, "MY_SOTA_REF", q.MySOTARef)
	writeField(bw, "MY_POTA_REF", q.MyPOTARef)
	writeIntPtr(bw, "MY_DXCC", q.MyDXCC)
	writeIntPtr(bw, "MY_CQ_ZONE", q.MyCQZone)
	writeIntPtr(bw, "MY_ITU_ZONE", q.MyITUZone)
	writeFloatPtr(bw, "MY_LAT", q.MyLat, 6)
	writeFloatPtr(bw, "MY_LON", q.MyLon, 6)
	writeField(bw, "MY_STREET", q.MyStreet)
	writeField(bw, "MY_CITY", q.MyCity)
	writeField(bw, "MY_POSTAL_CODE", q.MyPostalCode)
	writeField(bw, "MY_RIG", q.MyRig)
	writeField(bw, "MY_ANTENNA", q.MyAntenna)

	// --- Misc ---
	writeFloatPtr(bw, "TX_PWR", q.TXPower, 1)
	writeField(bw, "COMMENT", q.Comment)
	writeField(bw, "NOTES", q.Notes)

	// --- ADIF 3.1.7 additional promoted fields ---
	writeField(bw, "SIG", q.SIG)
	writeField(bw, "SIG_INFO", q.SIGInfo)
	writeField(bw, "MY_SIG", q.MySIG)
	writeField(bw, "MY_SIG_INFO", q.MySIGInfo)
	writeField(bw, "WWFF_REF", q.WWFFRef)
	writeField(bw, "MY_WWFF_REF", q.MyWWFFRef)
	writeFloatPtr(bw, "DISTANCE", q.Distance, 1)
	writeFloatPtr(bw, "RX_PWR", q.RXPower, 1)
	writeFloatPtr(bw, "A_INDEX", q.AIndex, 0)
	writeFloatPtr(bw, "K_INDEX", q.KIndex, 0)
	writeFloatPtr(bw, "SFI", q.SFI, 0)
	writeField(bw, "SKCC", q.SKCC)
	writeField(bw, "FISTS", q.FISTS)
	writeField(bw, "TEN_TEN", q.TenTen)
	writeField(bw, "CONTACTED_OP", q.ContactedOp)
	writeField(bw, "EQ_CALL", q.EqCall)
	writeField(bw, "PFX", q.PFX)
	writeField(bw, "MY_NAME", q.MyName)
	writeField(bw, "CLASS", q.Class)
	writeField(bw, "DARC_DOK", q.DarcDOK)
	writeField(bw, "MY_DARC_DOK", q.MyDarcDOK)
	writeField(bw, "REGION", q.Region)
	writeField(bw, "SILENT_KEY", q.SilentKey)
	writeField(bw, "SWL", q.SWL)
	writeField(bw, "QSO_COMPLETE", q.QSOComplete)
	writeField(bw, "QSO_RANDOM", q.QSORandom)
	writeField(bw, "CREDIT_GRANTED", q.CreditGranted)
	writeField(bw, "CREDIT_SUBMITTED", q.CreditSubmitted)
	writeField(bw, "MY_ARRL_SECT", q.MyARRLSect)
	writeField(bw, "MY_VUCC_GRIDS", q.MyVUCCGrids)

	// --- Extras (unpromoted ADIF fields preserved verbatim) ---
	// Standard mode emits ONLY valid ADIF-spec fields, so it drops APP_*
	// application-specific tags AND any non-standard / vendor tag — keeping
	// the file strictly portable to other loggers. Full mode keeps every
	// extra for a lossless OpsLog round-trip.
	for k, v := range q.Extras {
		tag := strings.ToUpper(k)
		if !includeApp && (strings.HasPrefix(tag, "APP_") || !IsStandardField(tag)) {
			continue
		}
		writeField(bw, tag, v)
	}

	bw.WriteString("<EOR>\n")
}

// writeField writes one `<TAG:length>value` pair, no-op when value is empty.
// length is the byte count (ADIF spec), which matches len(v) in Go since v is
// already a UTF-8 byte string.
func writeField(bw *bufio.Writer, tag, v string) {
	if v == "" {
		return
	}
	fmt.Fprintf(bw, "<%s:%d>%s ", tag, len(v), v)
}

func writeIntPtr(bw *bufio.Writer, tag string, p *int) {
	if p == nil {
		return
	}
	s := strconv.Itoa(*p)
	writeField(bw, tag, s)
}

func writeFloatPtr(bw *bufio.Writer, tag string, p *float64, decimals int) {
	if p == nil {
		return
	}
	s := strconv.FormatFloat(*p, 'f', decimals, 64)
	writeField(bw, tag, s)
}

// parentMode maps a "specific" mode (the ones we promote on import) back
// to its ADIF parent. Symmetric with promotableSubmodes in import.go.
var parentMode = map[string]string{
	"FT2": "MFSK", "FT4": "MFSK", "JS8": "MFSK", "MSK144": "MFSK",
	"ISCAT": "MFSK", "Q65": "MFSK", "FST4": "MFSK", "FST4W": "MFSK",
	"MFSK16": "MFSK", "MFSK32": "MFSK", "MFSK64": "MFSK", "MFSK128": "MFSK",
	"OLIVIA": "MFSK",
	"PSK31": "PSK", "PSK63": "PSK", "PSK125": "PSK", "PSK250": "PSK", "PSK500": "PSK",
	"QPSK31": "PSK", "QPSK63": "PSK", "QPSK125": "PSK", "QPSK250": "PSK", "QPSK500": "PSK",
	"FREEDV": "DIGITALVOICE",
	"VARA": "DYNAMIC", "VARA HF": "DYNAMIC", "VARA FM": "DYNAMIC", "VARAC": "DYNAMIC",
	"THOR4": "THOR", "THOR8": "THOR", "THOR16": "THOR", "THOR32": "THOR",
	"DOMINOF": "DOMINO", "DOMINOEX": "DOMINO",
	"HELL80": "HELL", "FMHELL": "HELL",
}

// modeForExport returns the (MODE, SUBMODE) pair to write. If we promoted
// on import (Mode=FT4 Submode=""), we re-derive the parent so the file
// is import-compatible with strict ADIF tools.
func modeForExport(mode, submode string) (string, string) {
	if submode != "" {
		// Already a (parent, child) pair — pass through unchanged.
		return mode, submode
	}
	if parent, ok := parentMode[mode]; ok {
		return parent, mode
	}
	return mode, ""
}
