#ifndef SENSOR_LOCATION_H
#define SENSOR_LOCATION_H

// ============================================================================
// Tier-1 RID Sensor — Sensor Position (live GPS, falling back to a static
// NVS-persisted value)
//
// UPDATED 2026-07-26: location_get() now prefers a live fix from the L76K
// GPS module (gps_module.h) when one is available, so units with a GPS
// wired in report their own real, current position — not just a value
// someone manually pushed once. If there's no live fix (module still
// acquiring lock, no GPS wired on a given unit, or the fix has gone stale),
// it falls back to exactly the prior behavior: a static deployment lat/lon
// persisted in NVS (namespace "pdl_loc") set via a SET_LOCATION command.
// Both paths use fixed-point integers (degrees * 1e7) — same scale used in
// DetectionEvent.
//
// Typical flow (unchanged, still works with no GPS present):
//   1. T2 sends a SET_LOCATION command with 8-byte params: int32_le lat | int32_le lon
//   2. command_handler dispatches to location_set()
//   3. Every main-loop detection-event build calls location_get() to populate
//      evt.lat_fixed / evt.lon_fixed instead of hard-coding zero.
//
// #ifdef ARDUINO guards surround all NVS/GPS calls so the module compiles
// cleanly in the native (host) test environment, where a static in-RAM
// store is used and gps_has_fix() always reports false.
// ============================================================================

#include <cstdint>
#include <cstdbool>

// ---------------------------------------------------------------------------
// Initialize the location subsystem.
// Opens the NVS namespace and loads persisted values into RAM shadow.
// Must be called from setup() before the main loop.
// Returns true on success; false if NVS init fails (sensor continues with
// default zero coordinates).
// ---------------------------------------------------------------------------
bool location_init();

// ---------------------------------------------------------------------------
// Persist new coordinates.  Values are written to NVS and the in-RAM shadow
// is updated atomically.
//   lat_fixed — latitude  × 1e7  (e.g.  38.9072° →  389072000)
//   lon_fixed — longitude × 1e7  (e.g. -77.0369° → -770369000)
// Returns true if NVS write succeeds (or if running in native test env).
// ---------------------------------------------------------------------------
bool location_set(int32_t lat_fixed, int32_t lon_fixed);

// ---------------------------------------------------------------------------
// Retrieve current coordinates from the in-RAM shadow.  Both pointers must
// be non-null.  Returns zero for both axes if location has never been set.
// ---------------------------------------------------------------------------
void location_get(int32_t* lat, int32_t* lon);

// ---------------------------------------------------------------------------
// Resolve the position to publish for ONE outbound report, in degrees.
//
// SINGLE SOURCE OF TRUTH for every reporting path — C2, MQTT, and any future
// Reticulum/rmap announce. This logic previously lived only inside
// c2_wifi_bridge.cpp (resolve_detection_latlon), which meant the same
// detection could reach C2 with a real position and MQTT with 0,0 — the
// MQTT payload published evt.lat_fixed/lon_fixed raw, with no fallback.
//
// Resolution order:
//   1. The event's own coordinates, if they are non-zero and in WGS84 range.
//      That covers a decoded drone position from an ODID broadcast, and a
//      relayed peer's own position.
//   2. Otherwise THIS unit's position: live GPS fix if one is held, else the
//      static value persisted by SET_LOCATION.
//
// Returns false when neither is available (no GPS, nothing ever set) so the
// caller can decide whether to publish at all rather than silently emitting
// 0,0 and putting a contact off the coast of Africa. Out params are left
// untouched on false.
//
// Rejects rather than clamps out-of-range input: clamping turns corrupt data
// into a plausible-looking contact at exactly 90.0 (see mesh_validate.h).
// ---------------------------------------------------------------------------
bool location_resolve_report(int32_t evt_lat_fixed, int32_t evt_lon_fixed,
                             double* out_lat, double* out_lon);

#endif // SENSOR_LOCATION_H
