#ifndef WATCHDOG_H
#define WATCHDOG_H

// ============================================================================
// Tier-1 RID Sensor — Hardware Watchdog + Crash Coredump Handler
//
// Wraps esp_task_wdt (task watchdog timer) and installs a panic handler that
// writes a binary coredump to LittleFS (/coredump_0.bin .. /coredump_2.bin),
// rotating with a maximum of WATCHDOG_MAX_COREDUMPS files.
//
// Usage:
//   1. Call watchdog_init(30) in setup() after all subsystems are ready.
//   2. Call watchdog_feed() once per main-loop iteration (near the top).
//   3. Call watchdog_register_panic_dump() after LittleFS is mounted so the
//      coredump handler has a filesystem to write to.
//
// #ifdef ARDUINO guards ensure the module stubs out gracefully in the native
// (host) unit-test environment.
// ============================================================================

#include <cstdint>

// Maximum number of coredump files to retain on LittleFS
#define WATCHDOG_MAX_COREDUMPS  3

// ---------------------------------------------------------------------------
// Initialize the task watchdog timer with the given timeout.
// Must be called from the main task (task0 / loop task) after subsystem inits.
// Default recommended value: 30 seconds.
// ---------------------------------------------------------------------------
void watchdog_init(uint32_t timeout_seconds);

// ---------------------------------------------------------------------------
// Feed (reset) the watchdog timer.  Call once per main-loop cycle.
// No-op in native test environment.
// ---------------------------------------------------------------------------
void watchdog_feed();

// ---------------------------------------------------------------------------
// Install the panic coredump handler.  Must be called after LittleFS.begin()
// so the handler can open files.  Registers an esp_panic_handler hook that
// captures the panic reason string and a short register-context snapshot.
// Files are named /coredump_0.bin, /coredump_1.bin, /coredump_2.bin; the
// oldest is overwritten when all slots are full (ring rotation).
// ---------------------------------------------------------------------------
void watchdog_register_panic_dump();

#endif // WATCHDOG_H
