package main

import (
	"bytes"
	"compress/gzip"
	"context"
	"crypto/tls"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"log/slog"
	"math/rand"
	"net"
	"net/http"
	"os"
	"path/filepath"
	"sort"
	"strconv"
	"strings"
	"sync"
	"sync/atomic"
	"time"
)

// Offline buffering. Undelivered messages wait in the spool (memory, plus one file each in
// <state_dir>/spool so they survive a restart) and go out oldest first once the server is
// reachable again. The spool is bounded in age, count and bytes:
//   - events, inventories and rollups are kept for 24 hours (an incident or a security signal
//     raised during an outage still arrives, and the charts get the outage's minutes);
//   - only the newest heartbeat is kept (an old one says nothing the newer one does not);
//   - at most spoolMaxItems messages and spoolMaxBytes bytes: past that the oldest heartbeats and
//     rollups go first, events last.
var (
	spoolMaxAge       = 24 * time.Hour
	spoolMaxAgeLegacy = time.Hour // 0.1.0 spool files (no message type in the name)
	spoolMaxItems     = 1500
	spoolMaxBytes     = 8 << 20
	backoffMin        = 15 * time.Second
	backoffMax        = 5 * time.Minute
)

// Sender posts reports (gzip JSON). It only ever sends: the answer's body is discarded and the
// status code is the only thing read from it.
type Sender struct {
	cfg     Config
	version string
	client  *http.Client
	// Dry, when set, receives each message as indented JSON instead of the network (run --dry-run).
	Dry io.Writer
	// OnResync is called when the server answers 205 Reset Content: its copy of the inventory
	// differs from ours, so the agent sends the full lists once.
	OnResync func()

	mu       sync.Mutex
	queue    []spooled // oldest first
	bytes    int
	nextID   uint64
	backoff  time.Duration
	nextTry  time.Time
	rnd      *rand.Rand
	wake     chan struct{}
	flushing sync.Mutex

	sentBytes atomic.Int64
	sentMsgs  atomic.Int64
	dropped   atomic.Int64
}

type spooled struct {
	id   uint64
	at   time.Time
	typ  string // heartbeat | rollup | event | inventory | "" (a 0.1.0 spool file)
	body []byte // gzip JSON
	file string
}

func NewSender(cfg Config, version string) *Sender {
	tr := &http.Transport{
		Proxy:                 http.ProxyFromEnvironment, // HTTPS_PROXY in the unit's environment, if any
		DialContext:           (&net.Dialer{Timeout: 10 * time.Second, KeepAlive: 30 * time.Second}).DialContext,
		TLSClientConfig:       &tls.Config{MinVersion: tls.VersionTLS12},
		TLSHandshakeTimeout:   10 * time.Second,
		ResponseHeaderTimeout: 20 * time.Second,
		MaxIdleConns:          2,
		IdleConnTimeout:       90 * time.Second,
		ForceAttemptHTTP2:     true,
	}
	return &Sender{cfg: cfg, version: version, wake: make(chan struct{}, 1), rnd: rand.New(rand.NewSource(time.Now().UnixNano())),
		client: &http.Client{
			Transport: tr,
			Timeout:   30 * time.Second,
			// Redirects are not followed: the report goes to the configured URL or nowhere.
			CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse },
		}}
}

func (s *Sender) spoolDir() string { return filepath.Join(s.cfg.StateDir, "spool") }

func maxAge(typ string) time.Duration {
	if typ == "" || typ == "heartbeat" {
		return spoolMaxAgeLegacy
	}
	return spoolMaxAge
}

// spoolName: "<unix nanoseconds>-<type>.json.gz". 0.1.0 wrote "<unix nanoseconds>.json.gz".
func spoolName(at time.Time, typ string) string {
	return strconv.FormatInt(at.UnixNano(), 10) + "-" + typ + ".json.gz"
}

func parseSpoolName(name string) (time.Time, string, bool) {
	base, ok := strings.CutSuffix(name, ".json.gz")
	if !ok {
		return time.Time{}, "", false
	}
	ts, typ, _ := strings.Cut(base, "-")
	n, err := strconv.ParseInt(ts, 10, 64)
	if err != nil || n <= 0 {
		return time.Time{}, "", false
	}
	switch typ {
	case "", "heartbeat", "rollup", "event", "inventory":
		return time.Unix(0, n), typ, true
	}
	return time.Time{}, "", false
}

// LoadSpool picks up reports a previous run could not send.
func (s *Sender) LoadSpool() {
	if s.Dry != nil {
		return
	}
	ents, err := os.ReadDir(s.spoolDir())
	if err != nil {
		return
	}
	s.mu.Lock()
	defer s.mu.Unlock()
	now := time.Now()
	for i, e := range ents {
		p := filepath.Join(s.spoolDir(), e.Name())
		at, typ, ok := parseSpoolName(e.Name())
		if !ok || !e.Type().IsRegular() || now.Sub(at) > maxAge(typ) || i >= 4*spoolMaxItems {
			_ = os.Remove(p) // half-written (.tmp), unknown or expired
			continue
		}
		b, err := readSmall(p, int64(spoolMaxBytes))
		if err != nil || len(b) < 2 || b[0] != 0x1f || b[1] != 0x8b {
			_ = os.Remove(p)
			continue
		}
		s.nextID++
		s.queue = append(s.queue, spooled{id: s.nextID, at: at, typ: typ, body: b, file: p})
		s.bytes += len(b)
	}
	sort.SliceStable(s.queue, func(i, j int) bool { return s.queue[i].at.Before(s.queue[j].at) })
	s.trimLocked(now)
}

func (s *Sender) Pending() int {
	s.mu.Lock()
	defer s.mu.Unlock()
	return len(s.queue)
}

func Encode(r any) ([]byte, error) {
	raw, err := json.Marshal(r)
	if err != nil {
		return nil, err
	}
	var buf bytes.Buffer
	buf.Grow(len(raw)/3 + 64)
	// The compressor's state is about 1 MB: it is reused, not allocated for every message.
	zw, _ := gzipPool.Get().(*gzip.Writer)
	if zw == nil {
		zw, _ = gzip.NewWriterLevel(&buf, gzip.BestCompression)
	} else {
		zw.Reset(&buf)
	}
	defer gzipPool.Put(zw)
	if _, err := zw.Write(raw); err != nil {
		return nil, err
	}
	if err := zw.Close(); err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}

var gzipPool sync.Pool

func msgKind(r any) string {
	if k, ok := r.(interface{ kind() string }); ok {
		return k.kind()
	}
	return "event"
}

// Enqueue adds a message to the spool and wakes the delivery loop. It never blocks on the network.
func (s *Sender) Enqueue(r any) {
	typ := msgKind(r)
	body, err := Encode(r)
	if err != nil {
		slog.Error("could not encode a report", "type", typ, "err", err)
		return
	}
	if s.Dry != nil {
		pretty, _ := json.MarshalIndent(r, "", "  ")
		fmt.Fprintf(s.Dry, "# %s message, %d bytes gzip (%d bytes JSON), not sent (--dry-run)\n%s\n", typ, len(body), len(pretty), pretty)
		s.sentMsgs.Add(1)
		s.sentBytes.Add(int64(len(body)))
		return
	}
	now := time.Now()
	s.mu.Lock()
	s.nextID++
	item := spooled{id: s.nextID, at: now, typ: typ, body: body}
	s.mu.Unlock()
	if err := os.MkdirAll(s.spoolDir(), 0o700); err == nil {
		p := filepath.Join(s.spoolDir(), spoolName(now, typ))
		if err := writeFileAtomic(p, body, 0o600); err == nil {
			item.file = p
		} else {
			logLimited("spool-write", time.Hour, slog.LevelWarn, "cannot write the spool; undelivered messages are kept in memory only", "err", err)
		}
	}
	s.mu.Lock()
	if typ == "heartbeat" { // superseded
		kept := s.queue[:0]
		for _, q := range s.queue {
			if q.typ == "heartbeat" {
				s.dropLocked(q, false)
				continue
			}
			kept = append(kept, q)
		}
		s.queue = kept
	}
	s.queue = append(s.queue, item)
	s.bytes += len(body)
	s.trimLocked(now)
	s.mu.Unlock()
	select {
	case s.wake <- struct{}{}:
	default:
	}
}

// trimLocked drops expired messages, then the least useful ones while the spool is over its caps.
func (s *Sender) trimLocked(now time.Time) {
	kept := s.queue[:0]
	for _, q := range s.queue {
		if now.Sub(q.at) > maxAge(q.typ) {
			s.dropLocked(q, true)
			continue
		}
		kept = append(kept, q)
	}
	s.queue = kept
	for len(s.queue) > 1 && (len(s.queue) > spoolMaxItems || s.bytes > spoolMaxBytes) {
		victim := 0 // the oldest message, unless an older-than-events heartbeat or rollup can go first
		for i, q := range s.queue {
			if q.typ == "heartbeat" || q.typ == "rollup" || q.typ == "" {
				victim = i
				break
			}
		}
		s.dropLocked(s.queue[victim], true)
		s.queue = append(s.queue[:victim], s.queue[victim+1:]...)
	}
}

func (s *Sender) dropLocked(q spooled, count bool) {
	if q.file != "" {
		_ = os.Remove(q.file)
	}
	s.bytes -= len(q.body)
	if count {
		s.dropped.Add(1)
		logLimited("spool-drop", 15*time.Minute, slog.LevelWarn, "dropped an undelivered message (spool limit)", "type", q.typ,
			"age", time.Since(q.at).Round(time.Second).String(), "queued", len(s.queue))
	}
}

func (s *Sender) remove(id uint64) {
	s.mu.Lock()
	defer s.mu.Unlock()
	for i, q := range s.queue {
		if q.id == id {
			s.dropLocked(q, false)
			s.queue = append(s.queue[:i], s.queue[i+1:]...)
			return
		}
	}
}

// Run delivers the spool until ctx ends: at once when a message is queued, and after a failure
// with exponential backoff (15 s doubling to 5 minutes, with jitter so that a fleet of agents does
// not retry in step when approvalens.com comes back).
func (s *Sender) Run(ctx context.Context) {
	for {
		s.mu.Lock()
		wait := time.Until(s.nextTry)
		s.mu.Unlock()
		if wait > 0 {
			t := time.NewTimer(wait)
			select {
			case <-ctx.Done():
				t.Stop()
				return
			case <-t.C:
			}
		}
		if !safeBool("send", func() bool { return s.Flush(ctx) }) {
			continue
		}
		select {
		case <-ctx.Done():
			return
		case <-s.wake:
		}
	}
}

// FinalFlush tries once more on shutdown (unless the server was unreachable a moment ago), for at
// most `timeout`. What is not delivered stays in the spool for the next start.
func (s *Sender) FinalFlush(timeout time.Duration) {
	s.mu.Lock()
	backingOff := time.Now().Before(s.nextTry)
	s.mu.Unlock()
	if backingOff || s.Pending() == 0 {
		return
	}
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()
	s.Flush(ctx)
}

var (
	errRetry   = errors.New("retry later")
	errAuth    = errors.New("token rejected")
	errRefused = errors.New("report refused")
)

// Flush sends the queue in order. It returns false when it stopped on a temporary failure (network
// error, 429, 5xx); the next attempt then waits for the backoff.
func (s *Sender) Flush(ctx context.Context) bool {
	s.flushing.Lock()
	defer s.flushing.Unlock()
	for {
		s.mu.Lock()
		s.trimLocked(time.Now())
		if len(s.queue) == 0 {
			s.mu.Unlock()
			return true
		}
		item := s.queue[0]
		s.mu.Unlock()
		err := s.post(ctx, item.body)
		switch {
		case err == nil:
			s.remove(item.id)
			s.mu.Lock()
			s.backoff, s.nextTry = 0, time.Time{}
			s.mu.Unlock()
		case errors.Is(err, errRetry):
			if ctx.Err() != nil { // shutting down: not the server's fault, no backoff
				return false
			}
			d := s.fail()
			logLimited("send-retry", 15*time.Minute, slog.LevelWarn, "report not delivered, kept in the spool", "queued", s.Pending(),
				"retry_in", d.Round(time.Second).String(), "err", err)
			return false
		case errors.Is(err, errAuth):
			s.remove(item.id)
			s.fail()
			logLimited("send-auth", 15*time.Minute, slog.LevelError, "the server rejected the token: was the server deleted from the account? "+
				"Check the token in "+s.cfg.path, "err", err)
			return false
		default: // refused (400 / 413): this message will never be accepted
			s.remove(item.id)
			logLimited("send-refused", 15*time.Minute, slog.LevelWarn, "report refused and dropped", "type", item.typ, "err", err)
		}
	}
}

// fail doubles the backoff and sets the next attempt (half the backoff plus a random part).
func (s *Sender) fail() time.Duration {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.backoff = min(max(2*s.backoff, backoffMin), backoffMax)
	d := s.backoff/2 + time.Duration(s.rnd.Int63n(int64(s.backoff/2)+1))
	s.nextTry = time.Now().Add(d)
	return d
}

func (s *Sender) post(ctx context.Context, body []byte) error {
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, s.cfg.Endpoint(), bytes.NewReader(body))
	if err != nil {
		return err
	}
	req.Header.Set("Authorization", "Bearer "+s.cfg.Token)
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Content-Encoding", "gzip")
	req.Header.Set("User-Agent", "approvalens-agent/"+s.version)
	res, err := s.client.Do(req)
	if err != nil {
		return fmt.Errorf("%w: %v", errRetry, err)
	}
	_, _ = io.Copy(io.Discard, io.LimitReader(res.Body, 4096))
	res.Body.Close()
	switch {
	case res.StatusCode >= 200 && res.StatusCode < 300:
		s.sentBytes.Add(int64(len(body)))
		s.sentMsgs.Add(1)
		if res.StatusCode == http.StatusResetContent && s.OnResync != nil {
			s.OnResync()
		}
		return nil
	case res.StatusCode == 401 || res.StatusCode == 403:
		return fmt.Errorf("%w (HTTP %d)", errAuth, res.StatusCode)
	case res.StatusCode == 413 || res.StatusCode == 400:
		return fmt.Errorf("%w (HTTP %d)", errRefused, res.StatusCode)
	default:
		return fmt.Errorf("%w: HTTP %d", errRetry, res.StatusCode)
	}
}

// Sent returns the messages and bytes (compressed, as posted) delivered since start.
func (s *Sender) Sent() (msgs, bytes int64) { return s.sentMsgs.Load(), s.sentBytes.Load() }
