// Package offlineq is OpsLog's offline safety net.
//
// When the shared MySQL logbook is unreachable, a QSO must never be lost just
// because the network blinked. Instead of failing, the QSO is appended to a
// local ADIF file (the "outbox") and replayed into the database as soon as it
// comes back — then the file is ARCHIVED, never deleted.
//
// Deliberately NOT a sync engine: it only ever PUSHES the operator's own QSOs.
// There is no mirror, no pull, no merge, no tombstones — which is exactly why it
// stays small. The cost, accepted by design: during an outage you don't see other
// operators' QSOs and the worked-before check doesn't know about your pending
// ones. See the queue view in the UI for what's waiting.
//
// The file lives in OpsLog's data directory — NEVER in a cloud-synced folder
// (Seafile/OneDrive): replicating a live file byte-by-byte is what we're
// escaping in the first place.
package offlineq

import (
	"crypto/rand"
	"encoding/hex"
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"sync"
	"time"

	"hamlog/internal/adif"
	"hamlog/internal/qso"
)

// FileName is the outbox. Pending QSOs accumulate here while the DB is down.
const FileName = "opslog-pending.adi"

// Queue owns the outbox file. All operations are serialised: the logging path
// (append) and the replay loop (read/rewrite/archive) run on different
// goroutines.
type Queue struct {
	dir string
	mu  sync.Mutex
}

// New returns a queue backed by <dir>/opslog-pending.adi.
func New(dir string) *Queue { return &Queue{dir: strings.TrimSpace(dir)} }

// Path is the outbox file's location (shown in the UI so the operator always
// knows where their QSOs physically are).
func (q *Queue) Path() string { return filepath.Join(q.dir, FileName) }

// newQueueID mints the id that makes replay idempotent.
func newQueueID() string {
	var b [12]byte
	if _, err := rand.Read(b[:]); err != nil {
		return fmt.Sprintf("t%d", time.Now().UnixNano())
	}
	return hex.EncodeToString(b[:])
}

// Append parks a QSO in the outbox, stamping it with a queue id (returned) so a
// repeated replay can recognise it. The file is created with an ADIF header on
// first use, then appended to — an append can't corrupt what's already there.
func (q *Queue) Append(rec qso.QSO) (string, error) {
	q.mu.Lock()
	defer q.mu.Unlock()

	if rec.Extras == nil {
		rec.Extras = map[string]string{}
	}
	qid := strings.TrimSpace(rec.Extras[qso.OfflineQueueKey])
	if qid == "" {
		qid = newQueueID()
		rec.Extras[qso.OfflineQueueKey] = qid
	}

	if err := os.MkdirAll(q.dir, 0o755); err != nil {
		return "", fmt.Errorf("offlineq: create dir: %w", err)
	}
	path := q.Path()
	_, statErr := os.Stat(path)
	f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
	if err != nil {
		return "", fmt.Errorf("offlineq: open %s: %w", path, err)
	}
	defer f.Close()

	var b strings.Builder
	if os.IsNotExist(statErr) { // brand-new file → write the ADIF header once
		b.WriteString("OpsLog offline queue — QSOs logged while the database was unreachable.\n")
		b.WriteString("<ADIF_VER:5>3.1.4 <PROGRAMID:6>OpsLog <EOH>\n\n")
	}
	b.WriteString(strings.TrimRight(adif.FullRecordADIF(rec), "\r\n"))
	b.WriteString("\n")

	if _, err := f.WriteString(b.String()); err != nil {
		return "", fmt.Errorf("offlineq: write: %w", err)
	}
	// Flush to disk: the whole point is surviving a crash/power cut.
	if err := f.Sync(); err != nil {
		return "", fmt.Errorf("offlineq: sync: %w", err)
	}
	return qid, nil
}

// Pending parses the outbox into QSOs (each carrying its queue id in Extras).
// A missing file simply means nothing is pending.
func (q *Queue) Pending() ([]qso.QSO, error) {
	q.mu.Lock()
	defer q.mu.Unlock()
	return q.pendingLocked()
}

func (q *Queue) pendingLocked() ([]qso.QSO, error) {
	f, err := os.Open(q.Path())
	if err != nil {
		if os.IsNotExist(err) {
			return nil, nil
		}
		return nil, err
	}
	defer f.Close()

	var out []qso.QSO
	err = adif.Parse(f, func(rec adif.Record) error {
		if v, ok := adif.RecordToQSO(rec); ok {
			out = append(out, v)
		}
		return nil
	})
	if err != nil {
		return out, fmt.Errorf("offlineq: parse %s: %w", q.Path(), err)
	}
	return out, nil
}

// Count is how many QSOs are waiting (0 when the file is absent).
func (q *Queue) Count() int {
	p, err := q.Pending()
	if err != nil {
		return 0
	}
	return len(p)
}

// Rewrite replaces the outbox with exactly these QSOs — used after a replay to
// keep only the ones that FAILED. Written to a temp file and renamed, so a crash
// mid-write can't truncate the queue.
func (q *Queue) Rewrite(keep []qso.QSO) error {
	q.mu.Lock()
	defer q.mu.Unlock()
	return q.rewriteLocked(keep)
}

func (q *Queue) rewriteLocked(keep []qso.QSO) error {
	path := q.Path()
	if len(keep) == 0 {
		// Nothing left: remove the (now empty) outbox. The caller archives the
		// original content first, so this never destroys the only copy.
		if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
			return fmt.Errorf("offlineq: remove: %w", err)
		}
		return nil
	}
	var b strings.Builder
	b.WriteString("OpsLog offline queue — QSOs logged while the database was unreachable.\n")
	b.WriteString("<ADIF_VER:5>3.1.4 <PROGRAMID:6>OpsLog <EOH>\n\n")
	for _, v := range keep {
		b.WriteString(strings.TrimRight(adif.FullRecordADIF(v), "\r\n"))
		b.WriteString("\n")
	}
	tmp := path + ".tmp"
	if err := os.WriteFile(tmp, []byte(b.String()), 0o644); err != nil {
		return fmt.Errorf("offlineq: write temp: %w", err)
	}
	if err := os.Rename(tmp, path); err != nil {
		return fmt.Errorf("offlineq: replace: %w", err)
	}
	return nil
}

// Archive copies the outbox to a timestamped file BEFORE it is cleared, so the
// QSOs always exist somewhere on disk even if the replay later turns out to have
// gone wrong. Deleting the only copy of someone's contacts is the one mistake
// you don't get to undo.
func (q *Queue) Archive() (string, error) {
	q.mu.Lock()
	defer q.mu.Unlock()

	data, err := os.ReadFile(q.Path())
	if err != nil {
		if os.IsNotExist(err) {
			return "", nil
		}
		return "", err
	}
	name := fmt.Sprintf("opslog-pending-%s.adi", time.Now().Format("2006-01-02-1504"))
	dst := filepath.Join(q.dir, name)
	if err := os.WriteFile(dst, data, 0o644); err != nil {
		return "", fmt.Errorf("offlineq: archive: %w", err)
	}
	return dst, nil
}
