#ifndef PEER_RELAY_H
#define PEER_RELAY_H

#include "config.h"

// ============================================================================
// peer_relay.h — Sensor-to-sensor detection forwarding over WiFi.
//
// WHAT THIS IS FOR
// ----------------
// Until now a sensor had exactly two ways to get a detection to C2:
//   1. POST it directly (needs its own WiFi + C2 endpoint), or
//   2. Broadcast it over LoRa and hope a WiFi-connected peer relays it.
//
// Path 2 is lossy in practice — measured at 1-2 of 6 single-shot detections
// landing, because the radio scheduler only gives PDL ~80% of the duty cycle
// and a one-shot 33-byte packet can simply miss the listening window. Path 1
// requires the unit to reach the internet itself.
//
// This module adds a third: hand the detection to a peer over WiFi (TCP, so
// it either arrives or reports an error — no silent loss), and let that peer
// forward it to C2 exactly as it would a LoRa-relayed one.
//
// WHY THE TIMESTAMP STILL WORKS
// -----------------------------
// The receiving peer calls c2_wifi_bridge_post_relayed_detection(), which
// runs resolve_detection_ts() on the RELAY's clock. So a clockless sender
// still produces a correctly-stamped, map-visible detection — the same reason
// the LoRa relay path works for a GPS-less V3.
//
// DUPLICATE SUPPRESSION
// ---------------------
// This is deliberately a FALLBACK, not an additional broadcast. main.cpp only
// calls it when the direct C2 post did not succeed. Without that ordering a
// WiFi-connected unit would post its own detection AND have a peer post it
// again, double-counting every contact on the operator map.
//
// Note this does not eliminate duplicates entirely: a detection sent here can
// still ALSO be heard over LoRa by a different peer and relayed a second time.
// That ambiguity predates this module (any two peers in range both relay), and
// resolving it needs server-side dedup on (sensor_id, timestamp, signature),
// which is a C2 change and out of scope here.
// ============================================================================

// Restore host/port from NVS. Call once at boot.
void peer_relay_init();

// Point this unit at a peer's ingest endpoint and persist it (survives reboot
// and reflash). Pass host = nullptr or "" to disable forwarding.
void peer_relay_configure(const char* host, uint16_t port);

// True once a peer endpoint is configured.
bool peer_relay_is_configured();

// Forward a detection to the configured peer. Returns true only if the peer
// accepted it (HTTP 2xx). No-op returning false when unconfigured or WiFi is
// down — callers treat that as "fall through to LoRa", which still runs.
bool peer_relay_post_detection(const DetectionEvent& evt);

struct PeerRelayStats {
    uint32_t forwarded_ok;
    uint32_t forwarded_failed;
    uint32_t skipped_unconfigured;
};
void peer_relay_get_stats(PeerRelayStats* out);

// Endpoint path served by the receiving side (wifi_status_server.cpp).
#define PEER_RELAY_PATH  "/peer-detection"

#endif // PEER_RELAY_H
