#ifndef WIFI_STATUS_SERVER_H
#define WIFI_STATUS_SERVER_H

// ============================================================================
// wifi_status_server.h — Optional WiFi station + HTTP status endpoint
// (ADDITIVE, off by default).
//
// Lets a sensor that's already been joined to a WiFi network be polled for
// REPORT_STATUS-equivalent JSON (GPS fix state, WiFi ODID stats, uptime,
// RSSI) without a USB cable — e.g. once carried outdoors on battery power
// for a GPS sky-view test.
//
// Join a network via the serial console (USB, while still tethered):
//   WIFI_CONNECT <ssid> <password>
// Then, from any host on that same LAN:
//   GET http://<assigned-ip>/status
//
// Nothing here runs unless wifi_status_server_connect() is explicitly
// called — boards that never issue WIFI_CONNECT are unaffected.
//
// ---------------------------------------------------------------------------
// Auto-join (2026-07-28) — field-deployed sensors can't have someone typing
// WIFI_CONNECT over USB every time they reset. wifi_autojoin_tick(), called
// every loop() cycle, keeps the sensor trying to get itself online whenever
// it isn't already connected:
//   1. Round-robins through any networks saved via WIFI_REMEMBER <ssid>
//      <password> (persisted in NVS, survives reboot/reflash).
//   2. If none of those are in range/working, scans and joins the
//      strongest OPEN (unencrypted) network it can see — the sensor has no
//      way to know an arbitrary secured network's password, so "any wifi
//      connection or phone it can connect to" means whatever it can join
//      WITHOUT credentials.
// Retries on a timer (WIFI_AUTOJOIN_RETRY_MS) whenever disconnected, so it
// keeps trying as it moves in/out of range. A manual WIFI_CONNECT always
// takes priority — autojoin never fights an in-flight connection attempt.
// ============================================================================

void wifi_status_server_init();
void wifi_status_server_tick();
void wifi_status_server_connect(const char* ssid, const char* password);

// Load saved credentials from NVS and arm the autojoin state machine. Call
// once at boot, right after wifi_status_server_init().
void wifi_autojoin_init();

// Persist an SSID/password pair for future autojoin attempts (WIFI_REMEMBER
// console command). Up to WIFI_AUTOJOIN_MAX_SAVED slots; re-saving an
// existing SSID updates it in place, a full table overwrites the oldest
// slot. password may be "" for a network the sensor should treat as open.
void wifi_autojoin_remember(const char* ssid, const char* password);

// Drive the autojoin state machine — call every loop() cycle alongside
// wifi_status_server_tick(). No-op while already connected or while a
// connection attempt (autojoin's own or a manual WIFI_CONNECT) is in flight.
void wifi_autojoin_tick();

// Force an immediate autojoin attempt on the next tick rather than waiting
// for the retry timer (WIFI_AUTOJOIN console command).
void wifi_autojoin_trigger_now();

// ---------------------------------------------------------------------------
// Field WiFi provisioning (SoftAP + web form) -- 2026-07-30
//
// autojoin only ever tries saved credentials or an open AP -- it has no way
// to learn an arbitrary secured network's password. If the sensor has never
// connected to ANY network since boot and keeps finding nothing usable, a
// human needs to type real credentials in, but a serial cable isn't always
// available in the field. wifi_autojoin_tick() calls this automatically
// after WIFI_PROVISION_TRIGGER_FAILURES consecutive "nothing usable" cycles
// with zero prior successful connection ever -- never once the sensor has
// connected at least once, so a temporarily out-of-range *already-working*
// unit keeps retrying normally instead of dropping its own AP.
//
// Once active: the sensor stands up its own open AP ("PDL-<name>-Setup"),
// serves a plain HTML form at http://192.168.4.1/ (any phone/laptop, no app
// needed), and on submit calls wifi_autojoin_remember() +
// wifi_status_server_connect() with what was typed in, then tears the AP
// back down.
// ---------------------------------------------------------------------------

// Force provisioning mode on demand (WIFI_PROVISION console command, or a
// future BLE-triggered path) instead of waiting for the failure threshold.
void wifi_provisioning_start();

#endif // WIFI_STATUS_SERVER_H
