#include <Arduino.h>
#include "config.h"
#include "data_model.h"
#include "json_codec.h"
#include "payload_codec.h"
#include "compressor.h"
#include "lora_transport.h"
#include "meter_driver.h"
#include "meter_health.h"
#include "power_manager.h"
#include "time_manager.h"
#include "wifi_manager.h"
#include "mqtt_client.h"
#include "web_server.h"
#include "display_ui.h"
#include "test_mode.h"
#include "sd_logger.h"
#include "hil_trace.h"
#include <stdarg.h>
#include <math.h>
#ifdef ARDUINO_ARCH_ESP32
#include <esp_task_wdt.h>
#include <esp_system.h>
#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>
#include <freertos/task.h>
#endif

static DeviceRole g_role = DeviceRole::Sender;
static uint16_t g_short_id = 0;
static char g_device_id[16] = "";

static SenderStatus g_sender_statuses[NUM_SENDERS];
static bool g_ap_mode = false;
static WifiMqttConfig g_cfg;
static uint32_t g_last_timesync_ms = 0;
static constexpr uint32_t TIME_SYNC_OFFSET_MS = 15000;
static uint32_t g_boot_ms = 0;
static FaultCounters g_sender_faults = {};
static FaultCounters g_receiver_faults = {};
static FaultCounters g_receiver_faults_published = {};
static FaultCounters g_sender_faults_remote[NUM_SENDERS] = {};
static FaultCounters g_sender_faults_remote_published[NUM_SENDERS] = {};
static FaultType g_sender_last_error = FaultType::None;
static FaultType g_receiver_last_error = FaultType::None;
static FaultType g_sender_last_error_remote[NUM_SENDERS] = {};
static FaultType g_sender_last_error_remote_published[NUM_SENDERS] = {};
static FaultType g_receiver_last_error_published = FaultType::None;
static uint32_t g_sender_last_error_utc = 0;
static uint32_t g_sender_last_error_ms = 0;
static uint32_t g_receiver_last_error_utc = 0;
static uint32_t g_receiver_last_error_ms = 0;
static uint32_t g_sender_last_error_remote_utc[NUM_SENDERS] = {};
static uint32_t g_sender_last_error_remote_ms[NUM_SENDERS] = {};
static bool g_sender_discovery_sent[NUM_SENDERS] = {};
static bool g_receiver_discovery_sent = false;

struct TimeSyncBurstState {
  bool active;
  uint32_t start_ms;
  uint32_t last_send_ms;
  uint32_t last_drift_check_ms;
  bool last_drift_ok;
};

static TimeSyncBurstState g_timesync_burst[NUM_SENDERS] = {};
static uint32_t g_sender_last_timesync_rx_ms = 0;
static bool g_sender_timesync_error = false;

static constexpr size_t BATCH_HEADER_SIZE = 6;
static constexpr size_t BATCH_CHUNK_PAYLOAD = LORA_MAX_PAYLOAD - BATCH_HEADER_SIZE;
static constexpr size_t BATCH_MAX_COMPRESSED = 4096;
static constexpr uint32_t BATCH_RX_MARGIN_MS = 800;

struct BatchBuffer {
  uint16_t batch_id;
  bool batch_id_valid;
  uint8_t count;
  MeterData samples[METER_BATCH_MAX_SAMPLES];
};

static BatchBuffer g_batch_queue[BATCH_QUEUE_DEPTH];
static uint8_t g_batch_head = 0;
static uint8_t g_batch_tail = 0;
static uint8_t g_batch_count = 0;

static MeterData g_build_samples[METER_BATCH_MAX_SAMPLES];
static uint8_t g_build_count = 0;

static uint32_t g_last_sample_ms = 0;
static uint32_t g_last_sample_ts_utc = 0;
static uint32_t g_last_send_ms = 0;
static uint32_t g_last_batch_send_ms = 0;
static float g_last_battery_voltage_v = NAN;
static uint8_t g_last_battery_percent = 0;
static uint32_t g_last_battery_ms = 0;
static uint16_t g_batch_id = 1;
static uint16_t g_last_sent_batch_id = 0;
static uint16_t g_last_acked_batch_id = 0;
static uint8_t g_batch_retry_count = 0;
static bool g_batch_ack_pending = false;
static uint32_t g_batch_ack_timeout_ms = BATCH_ACK_TIMEOUT_MS;
static MeterData g_inflight_samples[METER_BATCH_MAX_SAMPLES];
static uint8_t g_inflight_count = 0;
static uint16_t g_inflight_batch_id = 0;
static bool g_inflight_active = false;
static uint32_t g_last_debug_log_ms = 0;
static uint32_t g_sender_last_timesync_check_ms = 0;
static uint32_t g_sender_rx_window_ms = 0;
static uint32_t g_sender_sleep_ms = 0;
static uint32_t g_sender_power_log_ms = 0;
static uint8_t g_sender_timesync_mode = 0;
static RxRejectReason g_sender_rx_reject_reason = RxRejectReason::None;
static uint32_t g_sender_rx_reject_log_ms = 0;
static MeterData g_last_meter_data = {};
static bool g_last_meter_valid = false;
static uint32_t g_last_meter_rx_ms = 0;
static uint32_t g_meter_stale_seconds = 0;
static MeterHealthState g_meter_health_state = {};
static constexpr uint32_t METER_SAMPLE_MAX_AGE_MS = 15000;
#ifdef ARDUINO_ARCH_ESP32
struct MeterSampleEvent {
  MeterData data;
  uint32_t rx_ms;
};
static QueueHandle_t g_meter_sample_queue = nullptr;
static TaskHandle_t g_meter_reader_task = nullptr;
static bool g_meter_reader_task_running = false;
#endif
static constexpr uint32_t SENDER_TIMESYNC_ACQUIRE_MS = 10UL * 60UL * 1000UL;
static constexpr uint32_t SENDER_TIMESYNC_ACQUIRE_INTERVAL_SEC = 20;
static constexpr uint32_t SENDER_TIMESYNC_ACQUIRE_WINDOW_MS = 3000;

static void watchdog_kick();

static void serial_debug_printf(const char *fmt, ...) {
  if (!SERIAL_DEBUG_MODE) {
    return;
  }
  char buf[256];
  va_list args;
  va_start(args, fmt);
  vsnprintf(buf, sizeof(buf), fmt, args);
  va_end(args);
  Serial.printf("%s\n", buf);
}

static void set_last_meter_sample(const MeterData &parsed, uint32_t rx_ms) {
  g_last_meter_data = parsed;
  g_last_meter_valid = true;
  g_last_meter_rx_ms = rx_ms;
  g_meter_stale_seconds = 0;
}

static bool poll_and_parse_meter(MeterData &parsed) {
  const char *frame = nullptr;
  size_t frame_len = 0;
  if (!meter_poll_frame(frame, frame_len)) return false;
  parsed = {};
  parsed.energy_total_kwh = NAN;
  parsed.total_power_w = NAN;
  parsed.phase_power_w[0] = NAN;
  parsed.phase_power_w[1] = NAN;
  parsed.phase_power_w[2] = NAN;
  parsed.valid = false;
  return meter_parse_frame(frame, frame_len, parsed);
}

#ifdef ARDUINO_ARCH_ESP32
static void meter_reader_task_entry(void *) {
  for (;;) {
    MeterData parsed = {};
    if (!poll_and_parse_meter(parsed)) {
      vTaskDelay(pdMS_TO_TICKS(5));
      continue;
    }
    MeterSampleEvent event = {};
    event.data = parsed;
    event.rx_ms = millis();
    xQueueOverwrite(g_meter_sample_queue, &event);
  }
}

static bool meter_reader_start() {
  g_meter_sample_queue = xQueueCreate(1, sizeof(MeterSampleEvent));
  if (!g_meter_sample_queue) return false;
  BaseType_t rc = xTaskCreatePinnedToCore(meter_reader_task_entry, "meter_reader", 4096,
                                          nullptr, 2, &g_meter_reader_task, 0);
  if (rc != pdPASS) {
    vQueueDelete(g_meter_sample_queue);
    g_meter_sample_queue = nullptr;
    return false;
  }
  g_meter_reader_task_running = true;
  serial_debug_printf("meter: reader task started rx_buffer=8192");
  return true;
}
#endif

static void meter_reader_pump(uint32_t now_ms) {
#ifdef ARDUINO_ARCH_ESP32
  if (g_meter_reader_task_running && g_meter_sample_queue) {
    MeterSampleEvent event = {};
    if (xQueueReceive(g_meter_sample_queue, &event, 0) == pdTRUE) {
      set_last_meter_sample(event.data, event.rx_ms);
    }
    return;
  }
#endif
  MeterData parsed = {};
  if (poll_and_parse_meter(parsed)) set_last_meter_sample(parsed, now_ms);
}

static void sender_set_timesync_mode(uint8_t mode) {
  if (g_sender_timesync_mode == mode) {
    return;
  }
  g_sender_timesync_mode = mode;
  if (SERIAL_DEBUG_MODE) {
    const char *label = mode == 2 ? "acquire" : (mode == 1 ? "slow" : "fast");
    serial_debug_printf("timesync: mode=%s", label);
  }
}

static uint16_t g_last_batch_id_rx[NUM_SENDERS] = {};

struct BatchRxState {
  bool active;
  uint16_t batch_id;
  uint8_t next_index;
  uint8_t expected_chunks;
  uint16_t total_len;
  uint16_t received_len;
  uint32_t last_rx_ms;
  uint32_t timeout_ms;
  uint8_t buffer[BATCH_MAX_COMPRESSED];
};

static BatchRxState g_batch_rx = {};

static void init_sender_statuses() {
  for (uint8_t i = 0; i < NUM_SENDERS; ++i) {
    g_sender_statuses[i] = {};
    g_sender_statuses[i].has_data = false;
    g_sender_statuses[i].last_update_ts_utc = 0;
    g_sender_statuses[i].last_data.short_id = EXPECTED_SENDER_IDS[i];
    snprintf(g_sender_statuses[i].last_data.device_id, sizeof(g_sender_statuses[i].last_data.device_id), "dd3-%04X", EXPECTED_SENDER_IDS[i]);
    g_sender_faults_remote[i] = {};
    g_sender_faults_remote_published[i] = {};
    g_sender_last_error_remote[i] = FaultType::None;
    g_sender_last_error_remote_published[i] = FaultType::None;
    g_sender_last_error_remote_utc[i] = 0;
    g_sender_last_error_remote_ms[i] = 0;
    g_sender_discovery_sent[i] = false;
  }
}

static void update_battery_cache() {
  MeterData tmp = {};
  read_battery(tmp);
  g_last_battery_voltage_v = tmp.battery_voltage_v;
  g_last_battery_percent = tmp.battery_percent;
  g_last_battery_ms = millis();
}

static bool battery_sample_due(uint32_t now_ms) {
  return g_last_battery_ms == 0 || now_ms - g_last_battery_ms >= BATTERY_SAMPLE_INTERVAL_MS;
}

static bool sender_timesync_window_due() {
  uint32_t interval_sec = SENDER_TIMESYNC_CHECK_SEC_FAST;
  bool in_acquire = (g_sender_last_timesync_rx_ms == 0) && (millis() - g_boot_ms < SENDER_TIMESYNC_ACQUIRE_MS);
  bool allow_slow = (millis() - g_boot_ms >= 60000UL) && time_is_synced() && time_rtc_present() &&
      (g_sender_last_timesync_rx_ms > 0);
  // RTC boot time is not evidence of receiving network TimeSync.
  if (in_acquire) {
    interval_sec = SENDER_TIMESYNC_ACQUIRE_INTERVAL_SEC;
    sender_set_timesync_mode(2);
  } else if (allow_slow) {
    interval_sec = SENDER_TIMESYNC_CHECK_SEC_SLOW;
    sender_set_timesync_mode(1);
  } else {
    sender_set_timesync_mode(0);
  }
  static uint32_t last_interval_sec = 0;
  if (last_interval_sec != interval_sec) {
    last_interval_sec = interval_sec;
    g_sender_last_timesync_check_ms = millis();
  }
  if (g_sender_last_timesync_check_ms == 0) {
    g_sender_last_timesync_check_ms = millis() - interval_sec * 1000UL;
  }
  uint32_t now_ms = millis();
  if (now_ms - g_sender_last_timesync_check_ms >= interval_sec * 1000UL) {
    g_sender_last_timesync_check_ms = now_ms;
    return true;
  }
  return false;
}

static bool batch_queue_remove_oldest(bool overflow_drop) {
  if (g_batch_count == 0) {
    return false;
  }
  bool dropped_inflight = g_inflight_active && g_batch_queue[g_batch_tail].batch_id_valid &&
      g_inflight_batch_id == g_batch_queue[g_batch_tail].batch_id;
  if (dropped_inflight) {
    g_batch_ack_pending = false;
    g_batch_retry_count = 0;
    g_inflight_active = false;
    g_inflight_count = 0;
    g_inflight_batch_id = 0;
  }
  g_batch_tail = (g_batch_tail + 1) % BATCH_QUEUE_DEPTH;
  g_batch_count--;
  if (overflow_drop) {
    hil_trace_eventf("queue_drop", "\"queue\":%u,\"inflight\":%s", g_batch_count,
                     dropped_inflight ? "true" : "false");
  } else {
    hil_trace_eventf("queue", "\"action\":\"dequeue_acked\",\"queue\":%u", g_batch_count);
  }
  return dropped_inflight;
}

static void receiver_note_timesync_drift(uint8_t sender_idx, uint32_t sender_ts_utc) {
  if (sender_idx >= NUM_SENDERS) {
    return;
  }
  if (!time_is_synced() || sender_ts_utc == 0) {
    return;
  }
  uint32_t now_utc = time_get_utc();
  uint32_t diff = now_utc > sender_ts_utc ? now_utc - sender_ts_utc : sender_ts_utc - now_utc;
  TimeSyncBurstState &state = g_timesync_burst[sender_idx];
  state.last_drift_check_ms = millis();
  state.last_drift_ok = diff <= TIME_SYNC_DRIFT_THRESHOLD_SEC;
  if (!state.last_drift_ok) {
    if (!state.active) {
      state.active = true;
      state.start_ms = millis();
      state.last_send_ms = 0;
    }
  }
}

static void sender_note_rx_reject(RxRejectReason reason, const char *context) {
  if (reason == RxRejectReason::None) {
    return;
  }
  g_sender_rx_reject_reason = reason;
  uint32_t now_ms = millis();
  if (SERIAL_DEBUG_MODE && now_ms - g_sender_rx_reject_log_ms >= 1000) {
    g_sender_rx_reject_log_ms = now_ms;
    serial_debug_printf("rx_reject: %s reason=%s", context, rx_reject_reason_text(reason));
  }
}

static BatchBuffer *batch_queue_peek() {
  if (g_batch_count == 0) {
    return nullptr;
  }
  return &g_batch_queue[g_batch_tail];
}

static void batch_queue_enqueue(const MeterData *samples, uint8_t count) {
  if (!samples || count == 0) {
    return;
  }
  if (g_batch_count >= BATCH_QUEUE_DEPTH) {
    if (batch_queue_remove_oldest(true)) {
      g_batch_id++;
    }
  }
  BatchBuffer &slot = g_batch_queue[g_batch_head];
  slot.batch_id = 0;
  slot.batch_id_valid = false;
  slot.count = count;
  for (uint8_t i = 0; i < count; ++i) {
    slot.samples[i] = samples[i];
  }
  g_batch_head = (g_batch_head + 1) % BATCH_QUEUE_DEPTH;
  g_batch_count++;
  hil_trace_eventf("queue", "\"action\":\"enqueue\",\"queue\":%u,\"samples\":%u",
                   g_batch_count, count);
}

static uint32_t last_sample_ts() {
  if (g_last_sample_ts_utc == 0) {
    uint32_t now_utc = time_get_utc();
    return now_utc > 0 ? now_utc : millis() / 1000;
  }
  return g_last_sample_ts_utc;
}

static void note_fault(FaultCounters &counters, FaultType &last_type, uint32_t &last_ts_utc, uint32_t &last_ts_ms, FaultType type) {
  if (type == FaultType::MeterRead) {
    counters.meter_read_fail++;
  } else if (type == FaultType::Decode) {
    counters.decode_fail++;
  } else if (type == FaultType::LoraTx) {
    counters.lora_tx_fail++;
  }
  last_type = type;
  last_ts_utc = time_get_utc();
  last_ts_ms = millis();
}

static uint32_t age_seconds(uint32_t ts_utc, uint32_t ts_ms) {
  if (time_is_synced() && ts_utc > 0) {
    uint32_t now = time_get_utc();
    return now > ts_utc ? now - ts_utc : 0;
  }
  return (millis() - ts_ms) / 1000;
}

static bool counters_changed(const FaultCounters &a, const FaultCounters &b) {
  return a.meter_read_fail != b.meter_read_fail || a.decode_fail != b.decode_fail || a.lora_tx_fail != b.lora_tx_fail;
}

static void publish_faults_if_needed(const char *device_id, const FaultCounters &counters, FaultCounters &last_published,
                                     FaultType last_error, FaultType &last_error_published, uint32_t last_error_utc, uint32_t last_error_ms) {
  if (!mqtt_is_connected()) {
    return;
  }
  if (!counters_changed(counters, last_published) && last_error == last_error_published) {
    return;
  }
  uint32_t age = last_error != FaultType::None ? age_seconds(last_error_utc, last_error_ms) : 0;
  if (mqtt_publish_faults(device_id, counters, last_error, age)) {
    last_published = counters;
    last_error_published = last_error;
  }
}

#ifdef ARDUINO_ARCH_ESP32
static void watchdog_init() {
  esp_task_wdt_deinit();
  esp_task_wdt_config_t config = {};
  config.timeout_ms = WATCHDOG_TIMEOUT_SEC * 1000;
  config.idle_core_mask = 0;
  config.trigger_panic = true;
  esp_task_wdt_init(&config);
  esp_task_wdt_add(nullptr);
}

static void watchdog_kick() {
  esp_task_wdt_reset();
}
#else
static void watchdog_init() {}
static void watchdog_kick() {}
#endif

static void write_u16_le(uint8_t *dst, uint16_t value) {
  dst[0] = static_cast<uint8_t>(value & 0xFF);
  dst[1] = static_cast<uint8_t>((value >> 8) & 0xFF);
}

static uint16_t read_u16_le(const uint8_t *src) {
  return static_cast<uint16_t>(src[0]) | (static_cast<uint16_t>(src[1]) << 8);
}

static uint16_t sender_id_from_short_id(uint16_t short_id) {
  for (uint8_t i = 0; i < NUM_SENDERS; ++i) {
    if (EXPECTED_SENDER_IDS[i] == short_id) {
      return static_cast<uint16_t>(i + 1);
    }
  }
  return 0;
}

static uint16_t short_id_from_sender_id(uint16_t sender_id) {
  if (sender_id == 0 || sender_id > NUM_SENDERS) {
    return 0;
  }
  return EXPECTED_SENDER_IDS[sender_id - 1];
}

static uint32_t kwh_to_wh_from_float(float value) {
  if (isnan(value)) {
    return 0;
  }
  double wh = static_cast<double>(value) * 1000.0;
  if (wh < 0.0) {
    wh = 0.0;
  }
  if (wh > static_cast<double>(UINT32_MAX)) {
    wh = static_cast<double>(UINT32_MAX);
  }
  return static_cast<uint32_t>(llround(wh));
}

static bool float_to_i16_w(float value, int16_t &out) {
  if (isnan(value)) {
    out = 0;
    return true;
  }
  long rounded = lroundf(value);
  if (rounded < INT16_MIN || rounded > INT16_MAX) {
    return false;
  }
  out = static_cast<int16_t>(rounded);
  return true;
}

static uint16_t battery_mv_from_voltage(float value) {
  if (isnan(value) || value <= 0.0f) {
    return 0;
  }
  long mv = lroundf(value * 1000.0f);
  if (mv < 0) {
    mv = 0;
  }
  if (mv > UINT16_MAX) {
    mv = UINT16_MAX;
  }
  return static_cast<uint16_t>(mv);
}

static uint32_t compute_batch_rx_timeout_ms(uint16_t total_len, uint8_t chunk_count) {
  if (total_len == 0 || chunk_count == 0) {
    return 10000;
  }
  size_t max_chunk_payload = total_len > BATCH_CHUNK_PAYLOAD ? BATCH_CHUNK_PAYLOAD : total_len;
  size_t payload_len = BATCH_HEADER_SIZE + max_chunk_payload;
  size_t packet_len = 5 + payload_len + 2;
  uint32_t per_chunk_toa_ms = lora_airtime_ms(packet_len);
  uint32_t timeout_ms = static_cast<uint32_t>(chunk_count) * per_chunk_toa_ms + BATCH_RX_MARGIN_MS;
  return timeout_ms < 10000 ? 10000 : timeout_ms;
}

static uint32_t compute_batch_ack_timeout_ms(size_t payload_len) {
  if (payload_len == 0) {
    return 10000;
  }
  uint8_t chunk_count = static_cast<uint8_t>((payload_len + BATCH_CHUNK_PAYLOAD - 1) / BATCH_CHUNK_PAYLOAD);
  size_t packet_len = 5 + BATCH_HEADER_SIZE + (payload_len > BATCH_CHUNK_PAYLOAD ? BATCH_CHUNK_PAYLOAD : payload_len) + 2;
  uint32_t per_chunk_toa_ms = lora_airtime_ms(packet_len);
  uint32_t timeout_ms = static_cast<uint32_t>(chunk_count) * per_chunk_toa_ms + BATCH_RX_MARGIN_MS;
  return timeout_ms < 10000 ? 10000 : timeout_ms;
}

static bool send_batch_payload(const uint8_t *data, size_t len, uint32_t ts_for_display, uint16_t batch_id) {
  if (!data || len == 0 || len > BATCH_MAX_COMPRESSED) {
    return false;
  }
  uint8_t chunk_count = static_cast<uint8_t>((len + BATCH_CHUNK_PAYLOAD - 1) / BATCH_CHUNK_PAYLOAD);
  if (chunk_count == 0) {
    return false;
  }

  bool all_ok = true;
  size_t offset = 0;
  for (uint8_t i = 0; i < chunk_count; ++i) {
    size_t chunk_len = len - offset;
    if (chunk_len > BATCH_CHUNK_PAYLOAD) {
      chunk_len = BATCH_CHUNK_PAYLOAD;
    }
    LoraPacket pkt = {};
    pkt.protocol_version = PROTOCOL_VERSION;
    pkt.role = DeviceRole::Sender;
    pkt.device_id_short = g_short_id;
    pkt.payload_type = PayloadType::MeterBatch;
    pkt.payload_len = chunk_len + BATCH_HEADER_SIZE;

    uint8_t *payload = pkt.payload;
    write_u16_le(&payload[0], batch_id);
    payload[2] = i;
    payload[3] = chunk_count;
    write_u16_le(&payload[4], static_cast<uint16_t>(len));
    memcpy(&payload[BATCH_HEADER_SIZE], data + offset, chunk_len);

    watchdog_kick();
    uint32_t tx_start = millis();
    bool dropped = hil_fault_take(HilFault::DropChunk);
    bool duplicate = false;
    bool ok = dropped ? true : lora_send(pkt);
    if (!dropped && hil_fault_take(HilFault::DuplicateChunk)) {
      duplicate = true;
      delay(10);
      ok = lora_send(pkt) && ok;
    }
    uint32_t tx_ms = millis() - tx_start;
    hil_trace_eventf("chunk_tx", "\"batch_id\":%u,\"index\":%u,\"count\":%u,\"len\":%u,\"crc32\":%lu,\"ok\":%s,\"dropped\":%s,\"duplicate\":%s",
                     batch_id, i, chunk_count, static_cast<unsigned>(chunk_len),
                     static_cast<unsigned long>(hil_crc32(data + offset, chunk_len)), ok ? "true" : "false",
                     dropped ? "true" : "false", duplicate ? "true" : "false");
    if (dropped || duplicate) {
      hil_trace_eventf("fault_injected", "\"fault\":\"%s\",\"batch_id\":%u,\"index\":%u",
                       dropped ? "drop_chunk" : "duplicate_chunk", batch_id, i);
    }
    all_ok = all_ok && ok;
    if (!ok) {
      note_fault(g_sender_faults, g_sender_last_error, g_sender_last_error_utc, g_sender_last_error_ms, FaultType::LoraTx);
      display_set_last_error(g_sender_last_error, g_sender_last_error_utc, g_sender_last_error_ms);
    }
    if (SERIAL_DEBUG_MODE && tx_ms > 500) {
      serial_debug_printf("tx: chunk %u/%u took %lums ok=%u", static_cast<unsigned>(i + 1),
                          static_cast<unsigned>(chunk_count), static_cast<unsigned long>(tx_ms), ok ? 1 : 0);
    }
    offset += chunk_len;
    delay(10);
  }

  display_set_last_tx(all_ok, ts_for_display);
  return all_ok;
}

static void send_batch_ack(uint16_t batch_id, uint16_t sender_id) {
  if (hil_fault_take(HilFault::SuppressAck)) {
    hil_trace_eventf("fault_injected", "\"fault\":\"suppress_ack\",\"batch_id\":%u", batch_id);
    hil_trace_eventf("ack_tx", "\"batch_id\":%u,\"sender\":%u,\"ok\":false,\"suppressed\":true", batch_id, sender_id);
    lora_receive_continuous();
    return;
  }
  if (hil_fault_take(HilFault::DelayAck)) {
    uint32_t delay_ms = hil_fault_delay_ms();
    hil_trace_eventf("fault_injected", "\"fault\":\"delay_ack\",\"batch_id\":%u,\"delay_ms\":%lu",
                     batch_id, static_cast<unsigned long>(delay_ms));
    delay(delay_ms);
  }
  LoraPacket ack = {};
  ack.protocol_version = PROTOCOL_VERSION;
  ack.role = DeviceRole::Receiver;
  ack.device_id_short = g_short_id;
  ack.payload_type = PayloadType::Ack;
  ack.payload_len = 6;
  uint16_t transmitted_batch_id = batch_id;
  if (hil_fault_take(HilFault::WrongAckBatch)) {
    transmitted_batch_id = static_cast<uint16_t>(batch_id + 1);
    hil_trace_eventf("fault_injected", "\"fault\":\"wrong_ack_batch\",\"batch_id\":%u,\"sent_batch_id\":%u",
                     batch_id, transmitted_batch_id);
  }
  write_u16_le(&ack.payload[0], transmitted_batch_id);
  write_u16_le(&ack.payload[2], sender_id);
  write_u16_le(&ack.payload[4], g_short_id);
  uint8_t repeats = ACK_REPEAT_COUNT == 0 ? 1 : ACK_REPEAT_COUNT;
  bool all_ok = true;
  for (uint8_t i = 0; i < repeats; ++i) {
    if (SERIAL_DEBUG_MODE) {
      serial_debug_printf("ack: tx repeat %u/%u batch_id=%u", static_cast<unsigned>(i + 1),
                          static_cast<unsigned>(repeats), batch_id);
    }
    all_ok = lora_send(ack) && all_ok;
    if (i + 1 < repeats && ACK_REPEAT_DELAY_MS > 0) {
      delay(ACK_REPEAT_DELAY_MS);
    }
  }
  hil_trace_eventf("ack_tx", "\"batch_id\":%u,\"sent_batch_id\":%u,\"sender\":%u,\"repeats\":%u,\"ok\":%s",
                   batch_id, transmitted_batch_id, sender_id, repeats, all_ok ? "true" : "false");
  lora_receive_continuous();
}

static bool prepare_inflight_from_queue() {
  if (g_inflight_active) {
    return true;
  }
  BatchBuffer *batch = batch_queue_peek();
  if (!batch || batch->count == 0) {
    return false;
  }
  if (!batch->batch_id_valid) {
    batch->batch_id = g_batch_id;
    batch->batch_id_valid = true;
  }
  g_inflight_count = batch->count;
  g_inflight_batch_id = batch->batch_id;
  for (uint8_t i = 0; i < g_inflight_count; ++i) {
    g_inflight_samples[i] = batch->samples[i];
  }
  g_inflight_active = true;
  hil_trace_eventf("batch_created", "\"batch_id\":%u,\"samples\":%u,\"queue\":%u",
                   g_inflight_batch_id, g_inflight_count, g_batch_count);
  return true;
}

static bool send_inflight_batch(uint32_t ts_for_display) {
  if (!g_inflight_active || g_inflight_count == 0) {
    return false;
  }
  BatchInput input = {};
  input.sender_id = sender_id_from_short_id(g_short_id);
  input.batch_id = g_inflight_batch_id;
  input.t_last = g_inflight_samples[g_inflight_count - 1].ts_utc;
  uint32_t dt_s = METER_SAMPLE_INTERVAL_MS / 1000;
  input.dt_s = dt_s > 0 ? static_cast<uint8_t>(dt_s) : 1;
  input.n = g_inflight_count;
  input.battery_mV = battery_mv_from_voltage(g_inflight_samples[g_inflight_count - 1].battery_voltage_v);
  input.err_m = g_sender_faults.meter_read_fail > 255 ? 255 : static_cast<uint8_t>(g_sender_faults.meter_read_fail);
  input.err_d = g_sender_faults.decode_fail > 255 ? 255 : static_cast<uint8_t>(g_sender_faults.decode_fail);
  input.err_tx = g_sender_faults.lora_tx_fail > 255 ? 255 : static_cast<uint8_t>(g_sender_faults.lora_tx_fail);
  input.err_last = static_cast<uint8_t>(g_sender_last_error);
  input.err_rx_reject = static_cast<uint8_t>(g_sender_rx_reject_reason);
  for (uint8_t i = 0; i < g_inflight_count; ++i) {
    input.energy_wh[i] = kwh_to_wh_from_float(g_inflight_samples[i].energy_total_kwh);
    if (!float_to_i16_w(g_inflight_samples[i].phase_power_w[0], input.p1_w[i]) ||
        !float_to_i16_w(g_inflight_samples[i].phase_power_w[1], input.p2_w[i]) ||
        !float_to_i16_w(g_inflight_samples[i].phase_power_w[2], input.p3_w[i])) {
      return false;
    }
  }

  static uint8_t encoded[BATCH_MAX_COMPRESSED];
  size_t encoded_len = 0;
  uint32_t encode_start = millis();
  if (!encode_batch(input, encoded, sizeof(encoded), &encoded_len)) {
    hil_trace_eventf("payload_encode", "\"batch_id\":%u,\"ok\":false,\"samples\":%u",
                     g_inflight_batch_id, g_inflight_count);
    return false;
  }
  hil_trace_eventf("payload_encode", "\"batch_id\":%u,\"ok\":true,\"samples\":%u,\"len\":%u,\"crc32\":%lu",
                   g_inflight_batch_id, g_inflight_count, static_cast<unsigned>(encoded_len),
                   static_cast<unsigned long>(hil_crc32(encoded, encoded_len)));
  uint32_t encode_ms = millis() - encode_start;
  if (SERIAL_DEBUG_MODE) {
    serial_debug_printf("tx: batch_id=%u count=%u bin_len=%u", g_inflight_batch_id, g_inflight_count,
                        static_cast<unsigned>(encoded_len));
    if (encode_ms > 200) {
      serial_debug_printf("tx: encode took %lums", static_cast<unsigned long>(encode_ms));
    }
  }
  g_batch_ack_timeout_ms = compute_batch_ack_timeout_ms(encoded_len);

  uint32_t send_start = millis();
  bool ok = send_batch_payload(encoded, encoded_len, ts_for_display, g_inflight_batch_id);
  uint32_t send_ms = millis() - send_start;
  if (SERIAL_DEBUG_MODE && send_ms > 1000) {
    serial_debug_printf("tx: send batch took %lums", static_cast<unsigned long>(send_ms));
  }
  if (ok) {
    g_last_batch_send_ms = millis();
    serial_debug_printf("tx: sent batch_id=%u len=%u", g_inflight_batch_id, static_cast<unsigned>(encoded_len));
  } else {
    serial_debug_printf("tx: send failed batch_id=%u", g_inflight_batch_id);
  }
  return ok;
}

static bool send_meter_batch(uint32_t ts_for_display) {
  if (!prepare_inflight_from_queue()) {
    return false;
  }
  bool ok = send_inflight_batch(ts_for_display);
  if (ok) {
    g_last_sent_batch_id = g_inflight_batch_id;
    g_batch_ack_pending = true;
  } else {
    g_inflight_active = false;
    g_inflight_count = 0;
    g_inflight_batch_id = 0;
  }
  return ok;
}

static bool resend_inflight_batch(uint32_t ts_for_display) {
  if (!g_batch_ack_pending || !g_inflight_active || g_inflight_count == 0) {
    return false;
  }
  return send_inflight_batch(ts_for_display);
}

static void finish_inflight_batch() {
  if (g_batch_count > 0) {
    batch_queue_remove_oldest(false);
  }
  g_batch_ack_pending = false;
  g_batch_retry_count = 0;
  g_inflight_active = false;
  g_inflight_count = 0;
  g_inflight_batch_id = 0;
  g_batch_id++;
}

static void reset_batch_rx() {
  g_batch_rx.active = false;
  g_batch_rx.batch_id = 0;
  g_batch_rx.next_index = 0;
  g_batch_rx.expected_chunks = 0;
  g_batch_rx.total_len = 0;
  g_batch_rx.received_len = 0;
  g_batch_rx.last_rx_ms = 0;
  g_batch_rx.timeout_ms = 0;
}

static bool process_batch_packet(const LoraPacket &pkt, BatchInput &out_batch, bool &decode_error, uint16_t &out_batch_id) {
  decode_error = false;
  if (pkt.payload_len < BATCH_HEADER_SIZE) {
    return false;
  }
  uint16_t batch_id = read_u16_le(&pkt.payload[0]);
  uint8_t chunk_index = pkt.payload[2];
  uint8_t chunk_count = pkt.payload[3];
  uint16_t total_len = read_u16_le(&pkt.payload[4]);
  const uint8_t *chunk_data = &pkt.payload[BATCH_HEADER_SIZE];
  size_t chunk_len = pkt.payload_len - BATCH_HEADER_SIZE;
  uint32_t now_ms = millis();
  hil_trace_eventf("chunk_rx", "\"batch_id\":%u,\"index\":%u,\"count\":%u,\"len\":%u,\"crc32\":%lu",
                   batch_id, chunk_index, chunk_count, static_cast<unsigned>(chunk_len),
                   static_cast<unsigned long>(hil_crc32(chunk_data, chunk_len)));

  bool rx_timed_out = g_batch_rx.active && (now_ms - g_batch_rx.last_rx_ms > g_batch_rx.timeout_ms);
  bool batch_changed = g_batch_rx.active && batch_id != g_batch_rx.batch_id;
  if (rx_timed_out || batch_changed) {
    hil_trace_eventf("reassembly_incomplete", "\"batch_id\":%u,\"received\":%u,\"expected_chunks\":%u,\"reason\":\"%s\"",
                     g_batch_rx.batch_id, g_batch_rx.received_len, g_batch_rx.expected_chunks,
                     rx_timed_out ? "timeout" : "batch_changed");
  }
  if (!g_batch_rx.active || batch_changed || rx_timed_out) {
    if (chunk_index != 0) {
      hil_trace_eventf("reassembly_incomplete", "\"batch_id\":%u,\"index\":%u,\"reason\":\"missing_first_chunk\"",
                       batch_id, chunk_index);
      reset_batch_rx();
      return false;
    }
    if (total_len == 0 || total_len > BATCH_MAX_COMPRESSED || chunk_count == 0) {
      reset_batch_rx();
      return false;
    }
    g_batch_rx.active = true;
    g_batch_rx.batch_id = batch_id;
    g_batch_rx.expected_chunks = chunk_count;
    g_batch_rx.total_len = total_len;
    g_batch_rx.received_len = 0;
    g_batch_rx.next_index = 0;
    g_batch_rx.timeout_ms = compute_batch_rx_timeout_ms(total_len, chunk_count);
  }

  if (!g_batch_rx.active || chunk_index != g_batch_rx.next_index || chunk_count != g_batch_rx.expected_chunks) {
    hil_trace_eventf("reassembly_incomplete", "\"batch_id\":%u,\"index\":%u,\"expected_index\":%u,\"reason\":\"sequence\"",
                     batch_id, chunk_index, g_batch_rx.next_index);
    reset_batch_rx();
    return false;
  }

  if (g_batch_rx.received_len + chunk_len > g_batch_rx.total_len || g_batch_rx.received_len + chunk_len > BATCH_MAX_COMPRESSED) {
    hil_trace_eventf("reassembly_incomplete", "\"batch_id\":%u,\"reason\":\"length\"", batch_id);
    reset_batch_rx();
    return false;
  }

  memcpy(&g_batch_rx.buffer[g_batch_rx.received_len], chunk_data, chunk_len);
  g_batch_rx.received_len += static_cast<uint16_t>(chunk_len);
  g_batch_rx.next_index++;
  g_batch_rx.last_rx_ms = now_ms;

  if (g_batch_rx.next_index == g_batch_rx.expected_chunks && g_batch_rx.received_len == g_batch_rx.total_len) {
    if (!decode_batch(g_batch_rx.buffer, g_batch_rx.received_len, &out_batch)) {
      decode_error = true;
      hil_trace_eventf("payload_decode", "\"batch_id\":%u,\"ok\":false,\"len\":%u",
                       batch_id, g_batch_rx.received_len);
      reset_batch_rx();
      return false;
    }
    out_batch_id = batch_id;
    hil_trace_eventf("reassembly_complete", "\"batch_id\":%u,\"chunks\":%u,\"len\":%u,\"crc32\":%lu",
                     batch_id, g_batch_rx.expected_chunks, g_batch_rx.received_len,
                     static_cast<unsigned long>(hil_crc32(g_batch_rx.buffer, g_batch_rx.received_len)));
    hil_trace_eventf("payload_decode", "\"batch_id\":%u,\"ok\":true,\"samples\":%u", batch_id, out_batch.n);
    reset_batch_rx();
    return true;
  }

  return false;
}

void setup() {
  Serial.begin(115200);
  delay(200);
#ifdef PAYLOAD_CODEC_TEST
  payload_codec_self_test();
#endif

  watchdog_init();
  g_boot_ms = millis();
  g_role = detect_role();
  init_device_ids(g_short_id, g_device_id, sizeof(g_device_id));
  int reset_reason = 0;
#ifdef ARDUINO_ARCH_ESP32
  reset_reason = static_cast<int>(esp_reset_reason());
#endif
  hil_trace_init(g_role, g_short_id, reset_reason);
  display_set_role(g_role);
  if (SERIAL_DEBUG_MODE) {
#ifdef ARDUINO_ARCH_ESP32
    serial_debug_printf("boot: reset_reason=%d", static_cast<int>(esp_reset_reason()));
#endif
    serial_debug_printf("boot: role=%s short_id=%04X dev=%s", g_role == DeviceRole::Sender ? "sender" : "receiver",
                        g_short_id, g_device_id);
  }

  lora_init();
  display_init();
  time_rtc_init();
  time_try_load_from_rtc();
  display_set_self_ids(g_short_id, g_device_id);

  if (g_role == DeviceRole::Sender) {
    power_sender_init();
    power_configure_unused_pins_sender();
    meter_init();
#ifdef ARDUINO_ARCH_ESP32
    if (!meter_reader_start()) {
      serial_debug_printf("meter: reader task unavailable, using inline polling");
    }
#endif
    g_last_sample_ms = millis() - METER_SAMPLE_INTERVAL_MS;
    g_last_send_ms = millis();
    update_battery_cache();
  } else {
    power_receiver_init();
    lora_receive_continuous();
    pinMode(PIN_ROLE, INPUT); // release pulldown before SD uses GPIO14 as SCK
    sd_logger_init();
    wifi_manager_init();
    init_sender_statuses();
    display_set_sender_statuses(g_sender_statuses, NUM_SENDERS);

    bool has_cfg = wifi_load_config(g_cfg);
    if (has_cfg && wifi_connect_sta(g_cfg)) {
      g_ap_mode = false;
      time_receiver_init(g_cfg.ntp_server_1.c_str(), g_cfg.ntp_server_2.c_str());
      mqtt_init(g_cfg, g_device_id);
      web_server_set_config(g_cfg);
      web_server_set_sender_faults(g_sender_faults_remote, g_sender_last_error_remote);
      web_server_begin_sta(g_sender_statuses, NUM_SENDERS);
    } else {
      g_ap_mode = true;
      char ap_ssid[32];
      snprintf(ap_ssid, sizeof(ap_ssid), "%s%04X", AP_SSID_PREFIX, g_short_id);
      wifi_start_ap(ap_ssid, AP_PASSWORD);
      if (g_cfg.ntp_server_1.isEmpty()) {
        g_cfg.ntp_server_1 = "pool.ntp.org";
      }
      if (g_cfg.ntp_server_2.isEmpty()) {
        g_cfg.ntp_server_2 = "time.nist.gov";
      }
      web_server_set_config(g_cfg);
      web_server_set_sender_faults(g_sender_faults_remote, g_sender_last_error_remote);
      web_server_begin_ap(g_sender_statuses, NUM_SENDERS);
    }
  }
}

static void sender_loop() {
  watchdog_kick();
  hil_trace_poll_commands();
  uint32_t now_ms = millis();
  hil_trace_health(g_batch_count, g_build_count, g_batch_retry_count);
  display_set_sender_queue(g_batch_count, g_build_count > 0);
  display_set_sender_batches(g_last_acked_batch_id, g_batch_id);
  if (SERIAL_DEBUG_MODE && now_ms - g_last_debug_log_ms >= 5000) {
    g_last_debug_log_ms = now_ms;
    serial_debug_printf("state: Q=%u%s A=%u C=%u inflight=%u ack_pending=%u retries=%u",
                        g_batch_count,
                        g_build_count > 0 ? "+" : "",
                        g_last_acked_batch_id,
                        g_batch_id,
                        g_inflight_count,
                        g_batch_ack_pending ? 1 : 0,
                        g_batch_retry_count);
  }

  meter_reader_pump(now_ms);

  if (now_ms - g_last_sample_ms >= METER_SAMPLE_INTERVAL_MS) {
    g_last_sample_ms = now_ms;
    MeterData data = {};
    data.short_id = g_short_id;
    strncpy(data.device_id, g_device_id, sizeof(data.device_id));

    uint32_t meter_age_ms = g_last_meter_valid ? now_ms - g_last_meter_rx_ms : UINT32_MAX;
    MeterHealthEvaluation meter_health = meter_health_evaluate(
        g_meter_health_state, g_last_meter_valid, meter_age_ms, METER_SAMPLE_MAX_AGE_MS);
    bool meter_ok = meter_health.ok;
    if (g_last_meter_valid) {
      data.energy_total_kwh = g_last_meter_data.energy_total_kwh;
      data.total_power_w = g_last_meter_data.total_power_w;
      data.phase_power_w[0] = g_last_meter_data.phase_power_w[0];
      data.phase_power_w[1] = g_last_meter_data.phase_power_w[1];
      data.phase_power_w[2] = g_last_meter_data.phase_power_w[2];
      g_meter_stale_seconds = meter_age_ms >= 1000 ? (meter_age_ms / 1000) : 0;
    } else {
      g_meter_stale_seconds++;
    }
    if (meter_health.fault_started) {
      note_fault(g_sender_faults, g_sender_last_error, g_sender_last_error_utc, g_sender_last_error_ms, FaultType::MeterRead);
      display_set_last_error(g_sender_last_error, g_sender_last_error_utc, g_sender_last_error_ms);
      hil_trace_eventf("meter_fault", "\"state\":\"active\",\"classification\":\"%s\",\"stale_s\":%lu",
                       meter_health_classification_text(meter_health.classification),
                       static_cast<unsigned long>(g_meter_stale_seconds));
    } else if (meter_health.recovered) {
      hil_trace_event("meter_fault", "\"state\":\"recovered\"");
    }
    if (g_build_count == 0 && battery_sample_due(now_ms)) {
      update_battery_cache();
    }
    data.battery_voltage_v = g_last_battery_voltage_v;
    data.battery_percent = g_last_battery_percent;
    data.rx_reject_reason = static_cast<uint8_t>(g_sender_rx_reject_reason);

    uint32_t now_utc = time_get_utc();
    data.ts_utc = now_utc > 0 ? now_utc : millis() / 1000;
    data.valid = meter_ok;
    hil_trace_eventf("meter_sample", "\"ok\":%s,\"classification\":\"%s\",\"ts\":%lu,\"stale_s\":%lu",
                     meter_ok ? "true" : "false",
                     meter_health_classification_text(meter_health.classification),
                     static_cast<unsigned long>(data.ts_utc),
                     static_cast<unsigned long>(g_meter_stale_seconds));

    if (meter_ok) {
      g_last_sample_ts_utc = data.ts_utc;
      g_build_samples[g_build_count++] = data;
      if (g_build_count >= METER_BATCH_MAX_SAMPLES) {
        batch_queue_enqueue(g_build_samples, g_build_count);
        g_build_count = 0;
      }
    }
    display_set_last_meter(data);
    display_set_last_read(meter_ok, data.ts_utc);
  }

  if (!g_batch_ack_pending && now_ms - g_last_send_ms >= METER_SEND_INTERVAL_MS) {
    g_last_send_ms = now_ms;
    send_meter_batch(last_sample_ts());
  }

  if (g_batch_ack_pending) {
    LoraPacket ack_pkt = {};
    const uint32_t ack_len = 5 + 6 + 2;
    uint32_t ack_air_ms = lora_airtime_ms(ack_len);
    uint32_t ack_window_ms = ack_air_ms + 300;
    if (ack_window_ms < 1200) {
      ack_window_ms = 1200;
    }
    if (ack_window_ms > 4000) {
      ack_window_ms = 4000;
    }
    if (SERIAL_DEBUG_MODE) {
      serial_debug_printf("ack: rx window=%lu airtime=%lu", static_cast<unsigned long>(ack_window_ms),
                          static_cast<unsigned long>(ack_air_ms));
    }
    uint32_t rx_start = millis();
    bool got_ack = lora_receive_window(ack_pkt, ack_window_ms);
    if (!got_ack) {
      got_ack = lora_receive_window(ack_pkt, ack_window_ms / 2);
    }
    uint32_t rx_elapsed = millis() - rx_start;
    if (SERIAL_DEBUG_MODE) {
      g_sender_rx_window_ms += rx_elapsed;
    }
    if (!got_ack) {
      sender_note_rx_reject(lora_get_last_rx_reject_reason(), "ack");
    } else if (ack_pkt.role != DeviceRole::Receiver) {
      sender_note_rx_reject(RxRejectReason::WrongRole, "ack");
    } else if (ack_pkt.payload_type != PayloadType::Ack) {
      sender_note_rx_reject(RxRejectReason::WrongPayloadType, "ack");
    } else if (ack_pkt.payload_len < 6) {
      sender_note_rx_reject(RxRejectReason::LengthMismatch, "ack");
    } else {
      uint16_t ack_id = read_u16_le(ack_pkt.payload);
      uint16_t ack_sender = read_u16_le(&ack_pkt.payload[2]);
      uint16_t ack_receiver = read_u16_le(&ack_pkt.payload[4]);
      if (ack_sender == g_short_id && ack_receiver == ack_pkt.device_id_short &&
          g_batch_ack_pending && ack_id == g_last_sent_batch_id) {
        g_last_acked_batch_id = ack_id;
        hil_trace_eventf("ack_rx", "\"batch_id\":%u,\"ok\":true,\"receiver\":%u", ack_id, ack_receiver);
        serial_debug_printf("ack: rx ok batch_id=%u", ack_id);
        finish_inflight_batch();
      } else {
        if (ack_sender != g_short_id || ack_receiver != ack_pkt.device_id_short) {
          sender_note_rx_reject(RxRejectReason::DeviceIdMismatch, "ack");
        } else if (ack_id != g_last_sent_batch_id) {
          sender_note_rx_reject(RxRejectReason::BatchIdMismatch, "ack");
        }
        hil_trace_eventf("ack_rx", "\"batch_id\":%u,\"ok\":false,\"expected_batch_id\":%u,\"sender\":%u,\"receiver\":%u",
                         ack_id, g_last_sent_batch_id, ack_sender, ack_receiver);
        if (SERIAL_DEBUG_MODE) {
          serial_debug_printf("ack: reject batch_id=%u sender=%u receiver=%u exp_batch=%u exp_sender=%u",
                              ack_id, ack_sender, ack_receiver, g_last_sent_batch_id, g_short_id);
        }
      }
    }
  }

  bool timesync_due = (!g_batch_ack_pending && sender_timesync_window_due());
  if (timesync_due) {
    hil_trace_eventf("time_bootstrap", "\"stage\":\"request\",\"window_ms\":%lu",
                     static_cast<unsigned long>((g_sender_timesync_mode == 2) ? SENDER_TIMESYNC_ACQUIRE_WINDOW_MS : SENDER_TIMESYNC_WINDOW_MS));
    LoraPacket rx = {};
    uint32_t rx_start = millis();
    uint32_t window_ms = (g_sender_timesync_mode == 2) ? SENDER_TIMESYNC_ACQUIRE_WINDOW_MS : SENDER_TIMESYNC_WINDOW_MS;
    bool got = lora_receive_window(rx, window_ms);
    uint32_t rx_elapsed = millis() - rx_start;
    if (SERIAL_DEBUG_MODE) {
      g_sender_rx_window_ms += rx_elapsed;
    }
    if (!got) {
      sender_note_rx_reject(lora_get_last_rx_reject_reason(), "timesync");
      if (SERIAL_DEBUG_MODE) {
        serial_debug_printf("timesync: rx miss window_ms=%lu", static_cast<unsigned long>(window_ms));
      }
    } else if (rx.role != DeviceRole::Receiver) {
      sender_note_rx_reject(RxRejectReason::WrongRole, "timesync");
    } else if (rx.payload_type != PayloadType::TimeSync) {
      sender_note_rx_reject(RxRejectReason::WrongPayloadType, "timesync");
    } else if (time_handle_timesync_payload(rx.payload, rx.payload_len)) {
      g_sender_last_timesync_rx_ms = now_ms;
      if (g_sender_timesync_error) {
        g_sender_timesync_error = false;
        display_set_last_error(FaultType::None, 0, 0);
      }
      serial_debug_printf("timesync: rx ok window_ms=%lu", static_cast<unsigned long>(window_ms));
      hil_trace_eventf("time_bootstrap", "\"stage\":\"complete\",\"ok\":true,\"utc\":%lu,\"receiver\":%u",
                       static_cast<unsigned long>(time_get_utc()), rx.device_id_short);
    } else {
      sender_note_rx_reject(RxRejectReason::LengthMismatch, "timesync");
      if (SERIAL_DEBUG_MODE) {
        serial_debug_printf("timesync: rx miss window_ms=%lu", static_cast<unsigned long>(window_ms));
      }
    }
  }
  uint32_t timesync_age_ms = (g_sender_last_timesync_rx_ms > 0) ? (now_ms - g_sender_last_timesync_rx_ms)
                                                                : (now_ms - g_boot_ms);
  if (!g_sender_timesync_error && timesync_age_ms > TIME_SYNC_ERROR_TIMEOUT_MS) {
    g_sender_timesync_error = true;
    display_set_last_error(FaultType::TimeSync, time_get_utc(), now_ms);
  }
  if (!g_batch_ack_pending) {
    lora_sleep();
  }

  if (g_batch_ack_pending && (now_ms - g_last_batch_send_ms >= g_batch_ack_timeout_ms)) {
    if (g_batch_retry_count < BATCH_MAX_RETRIES) {
      g_batch_retry_count++;
      hil_trace_eventf("retry", "\"batch_id\":%u,\"retry\":%u,\"reason\":\"ack_timeout\"",
                       g_inflight_batch_id, g_batch_retry_count);
      serial_debug_printf("ack: timeout batch_id=%u retry=%u", g_inflight_batch_id, g_batch_retry_count);
      resend_inflight_batch(last_sample_ts());
    } else {
      hil_trace_eventf("timeout", "\"batch_id\":%u,\"retries\":%u,\"kind\":\"ack\"",
                       g_inflight_batch_id, g_batch_retry_count);
      serial_debug_printf("ack: failed batch_id=%u policy=%s", g_inflight_batch_id,
                          BATCH_RETRY_POLICY == BatchRetryPolicy::Drop ? "drop" : "keep");
      if (BATCH_RETRY_POLICY == BatchRetryPolicy::Drop) {
        finish_inflight_batch();
      } else {
        g_batch_ack_pending = false;
        g_batch_retry_count = 0;
        g_inflight_active = false;
        g_inflight_count = 0;
        g_inflight_batch_id = 0;
      }
      note_fault(g_sender_faults, g_sender_last_error, g_sender_last_error_utc, g_sender_last_error_ms, FaultType::LoraTx);
      display_set_last_error(g_sender_last_error, g_sender_last_error_utc, g_sender_last_error_ms);
    }
  }

  display_tick();

  uint32_t next_sample_due = g_last_sample_ms + METER_SAMPLE_INTERVAL_MS;
  uint32_t next_send_due = g_last_send_ms + METER_SEND_INTERVAL_MS;
  uint32_t next_due = next_sample_due < next_send_due ? next_sample_due : next_send_due;
  if (!g_batch_ack_pending && next_due > now_ms) {
    watchdog_kick();
    if (SERIAL_DEBUG_MODE) {
      g_sender_sleep_ms += (next_due - now_ms);
      if (now_ms - g_sender_power_log_ms >= 10000) {
        g_sender_power_log_ms = now_ms;
        serial_debug_printf("power: rx_ms=%lu sleep_ms=%lu", static_cast<unsigned long>(g_sender_rx_window_ms),
                            static_cast<unsigned long>(g_sender_sleep_ms));
      }
    }
    lora_sleep();
    light_sleep_ms(next_due - now_ms);
  }
}

static void receiver_loop() {
  watchdog_kick();
  hil_trace_poll_commands();
  hil_trace_health(0, 0, 0);
  if (g_last_timesync_ms == 0) {
    g_last_timesync_ms = millis() - (TIME_SYNC_INTERVAL_SEC * 1000UL - TIME_SYNC_OFFSET_MS);
  }
  LoraPacket pkt = {};
  if (lora_receive(pkt, 0) && pkt.protocol_version == PROTOCOL_VERSION) {
    if (pkt.payload_type == PayloadType::MeterData) {
      uint8_t decompressed[256];
      size_t decompressed_len = 0;
      if (!decompressBuffer(pkt.payload, pkt.payload_len, decompressed, sizeof(decompressed) - 1, decompressed_len)) {
        note_fault(g_receiver_faults, g_receiver_last_error, g_receiver_last_error_utc, g_receiver_last_error_ms, FaultType::Decode);
        display_set_last_error(g_receiver_last_error, g_receiver_last_error_utc, g_receiver_last_error_ms);
      } else {
        if (decompressed_len >= sizeof(decompressed)) {
          note_fault(g_receiver_faults, g_receiver_last_error, g_receiver_last_error_utc, g_receiver_last_error_ms, FaultType::Decode);
          display_set_last_error(g_receiver_last_error, g_receiver_last_error_utc, g_receiver_last_error_ms);
          return;
        }
        decompressed[decompressed_len] = '\0';
        MeterData data = {};
        if (jsonToMeterData(String(reinterpret_cast<const char *>(decompressed)), data)) {
          data.link_valid = true;
          data.link_rssi_dbm = pkt.rssi_dbm;
          data.link_snr_db = pkt.snr_db;
          sd_logger_log_sample(data, data.last_error != FaultType::None);
          for (uint8_t i = 0; i < NUM_SENDERS; ++i) {
            if (pkt.device_id_short == EXPECTED_SENDER_IDS[i]) {
              data.short_id = pkt.device_id_short;
              g_sender_statuses[i].last_data = data;
              g_sender_statuses[i].last_update_ts_utc = data.ts_utc;
              g_sender_statuses[i].has_data = true;
              g_sender_faults_remote[i].meter_read_fail = data.err_meter_read;
              g_sender_faults_remote[i].lora_tx_fail = data.err_lora_tx;
              receiver_note_timesync_drift(i, data.ts_utc);
              g_sender_last_error_remote[i] = data.last_error;
              g_sender_last_error_remote_utc[i] = time_get_utc();
              g_sender_last_error_remote_ms[i] = millis();
              mqtt_publish_state(data);
              if (ENABLE_HA_DISCOVERY && !g_sender_discovery_sent[i]) {
                g_sender_discovery_sent[i] = mqtt_publish_discovery(data.device_id);
              }
              publish_faults_if_needed(data.device_id, g_sender_faults_remote[i], g_sender_faults_remote_published[i],
                                       g_sender_last_error_remote[i], g_sender_last_error_remote_published[i],
                                       g_sender_last_error_remote_utc[i], g_sender_last_error_remote_ms[i]);
              break;
            }
          }
        } else {
          note_fault(g_receiver_faults, g_receiver_last_error, g_receiver_last_error_utc, g_receiver_last_error_ms, FaultType::Decode);
          display_set_last_error(g_receiver_last_error, g_receiver_last_error_utc, g_receiver_last_error_ms);
        }
      }
    } else if (pkt.payload_type == PayloadType::MeterBatch) {
      BatchInput batch = {};
      bool decode_error = false;
      uint16_t batch_id = 0;
      if (process_batch_packet(pkt, batch, decode_error, batch_id)) {
        MeterData samples[METER_BATCH_MAX_SAMPLES];
        size_t count = 0;
        int8_t sender_idx = -1;
        for (uint8_t i = 0; i < NUM_SENDERS; ++i) {
          if (pkt.device_id_short == EXPECTED_SENDER_IDS[i]) {
            sender_idx = static_cast<int8_t>(i);
            break;
          }
        }
        bool duplicate = sender_idx >= 0 && g_last_batch_id_rx[sender_idx] == batch_id;
        if (duplicate) {
          hil_trace_eventf("duplicate", "\"batch_id\":%u,\"sender\":%u", batch_id, pkt.device_id_short);
          send_batch_ack(batch_id, pkt.device_id_short);
        } else {
          g_last_batch_id_rx[sender_idx] = batch_id;
          send_batch_ack(batch_id, pkt.device_id_short);
          count = batch.n;
          if (count == 0 || count > METER_BATCH_MAX_SAMPLES) {
            note_fault(g_receiver_faults, g_receiver_last_error, g_receiver_last_error_utc, g_receiver_last_error_ms, FaultType::Decode);
            display_set_last_error(g_receiver_last_error, g_receiver_last_error_utc, g_receiver_last_error_ms);
            return;
          }
          uint16_t short_id = pkt.device_id_short;
          if (short_id == 0) {
            short_id = short_id_from_sender_id(batch.sender_id);
          }
          uint64_t span = static_cast<uint64_t>(batch.dt_s) * static_cast<uint64_t>(count - 1);
          if (batch.t_last < span) {
            note_fault(g_receiver_faults, g_receiver_last_error, g_receiver_last_error_utc, g_receiver_last_error_ms, FaultType::Decode);
            display_set_last_error(g_receiver_last_error, g_receiver_last_error_utc, g_receiver_last_error_ms);
            return;
          }
          uint32_t t_first = batch.t_last - static_cast<uint32_t>(span);
          float bat_v = batch.battery_mV > 0 ? static_cast<float>(batch.battery_mV) / 1000.0f : NAN;
          for (size_t s = 0; s < count; ++s) {
            MeterData &data = samples[s];
            data = {};
            data.short_id = short_id;
            if (short_id != 0) {
              snprintf(data.device_id, sizeof(data.device_id), "dd3-%04X", short_id);
            } else {
              snprintf(data.device_id, sizeof(data.device_id), "dd3-0000");
            }
            data.ts_utc = t_first + static_cast<uint32_t>(s) * batch.dt_s;
            data.energy_total_kwh = static_cast<float>(batch.energy_wh[s]) / 1000.0f;
            data.phase_power_w[0] = static_cast<float>(batch.p1_w[s]);
            data.phase_power_w[1] = static_cast<float>(batch.p2_w[s]);
            data.phase_power_w[2] = static_cast<float>(batch.p3_w[s]);
            data.total_power_w = data.phase_power_w[0] + data.phase_power_w[1] + data.phase_power_w[2];
            data.battery_voltage_v = bat_v;
            if (!isnan(bat_v)) {
              data.battery_percent = battery_percent_from_voltage(bat_v);
            } else {
              data.battery_percent = 0;
            }
            data.valid = true;
            data.link_valid = true;
            data.link_rssi_dbm = pkt.rssi_dbm;
            data.link_snr_db = pkt.snr_db;
            data.err_meter_read = batch.err_m;
            data.err_decode = batch.err_d;
            data.err_lora_tx = batch.err_tx;
            data.last_error = static_cast<FaultType>(batch.err_last);
            data.rx_reject_reason = batch.err_rx_reject;
            sd_logger_log_sample(data, (s + 1 == count) && data.last_error != FaultType::None);
          }

          if (sender_idx >= 0) {
            web_server_set_last_batch(static_cast<uint8_t>(sender_idx), samples, count);
            for (size_t s = 0; s < count; ++s) {
              samples[s].short_id = pkt.device_id_short;
              mqtt_publish_state(samples[s]);
            }
            if (count > 0) {
              g_sender_statuses[sender_idx].last_data = samples[count - 1];
              g_sender_statuses[sender_idx].last_update_ts_utc = samples[count - 1].ts_utc;
              g_sender_statuses[sender_idx].has_data = true;
              g_sender_faults_remote[sender_idx].meter_read_fail = samples[count - 1].err_meter_read;
              g_sender_faults_remote[sender_idx].lora_tx_fail = samples[count - 1].err_lora_tx;
              receiver_note_timesync_drift(static_cast<uint8_t>(sender_idx), samples[count - 1].ts_utc);
              g_sender_last_error_remote[sender_idx] = samples[count - 1].last_error;
              g_sender_last_error_remote_utc[sender_idx] = time_get_utc();
              g_sender_last_error_remote_ms[sender_idx] = millis();
              if (ENABLE_HA_DISCOVERY && !g_sender_discovery_sent[sender_idx]) {
                g_sender_discovery_sent[sender_idx] = mqtt_publish_discovery(samples[count - 1].device_id);
              }
              publish_faults_if_needed(samples[count - 1].device_id, g_sender_faults_remote[sender_idx], g_sender_faults_remote_published[sender_idx],
                                       g_sender_last_error_remote[sender_idx], g_sender_last_error_remote_published[sender_idx],
                                       g_sender_last_error_remote_utc[sender_idx], g_sender_last_error_remote_ms[sender_idx]);
            }
          }
        }
      } else if (decode_error) {
        note_fault(g_receiver_faults, g_receiver_last_error, g_receiver_last_error_utc, g_receiver_last_error_ms, FaultType::Decode);
        display_set_last_error(g_receiver_last_error, g_receiver_last_error_utc, g_receiver_last_error_ms);
      }
    }
  }

  uint32_t interval_sec = TIME_SYNC_INTERVAL_SEC;
  uint32_t now_ms = millis();
  if (!g_ap_mode) {
    bool burst_sent = false;
    for (uint8_t i = 0; i < NUM_SENDERS; ++i) {
      TimeSyncBurstState &state = g_timesync_burst[i];
      if (state.active) {
        if (now_ms - state.start_ms >= TIME_SYNC_BURST_DURATION_MS) {
          state.active = false;
        } else if (state.last_send_ms == 0 || now_ms - state.last_send_ms >= TIME_SYNC_BURST_INTERVAL_MS) {
          state.last_send_ms = now_ms;
          burst_sent = true;
        }
      }
    }
    if (burst_sent) {
      bool time_sent = time_send_timesync(g_short_id);
      if (!time_sent) {
        note_fault(g_receiver_faults, g_receiver_last_error, g_receiver_last_error_utc, g_receiver_last_error_ms, FaultType::LoraTx);
        display_set_last_error(g_receiver_last_error, g_receiver_last_error_utc, g_receiver_last_error_ms);
      }
      if (SERIAL_DEBUG_MODE) {
        serial_debug_printf("timesync: tx burst");
      }
      hil_trace_eventf("time_bootstrap", "\"stage\":\"transmit\",\"mode\":\"burst\",\"ok\":%s,\"utc\":%lu",
                       time_sent ? "true" : "false", static_cast<unsigned long>(time_get_utc()));
      g_last_timesync_ms = now_ms;
    } else if (now_ms - g_last_timesync_ms > interval_sec * 1000UL) {
      g_last_timesync_ms = now_ms;
      bool time_sent = time_send_timesync(g_short_id);
      if (!time_sent) {
        note_fault(g_receiver_faults, g_receiver_last_error, g_receiver_last_error_utc, g_receiver_last_error_ms, FaultType::LoraTx);
        display_set_last_error(g_receiver_last_error, g_receiver_last_error_utc, g_receiver_last_error_ms);
      }
      if (SERIAL_DEBUG_MODE) {
        serial_debug_printf("timesync: tx normal");
      }
      hil_trace_eventf("time_bootstrap", "\"stage\":\"transmit\",\"mode\":\"normal\",\"ok\":%s,\"utc\":%lu",
                       time_sent ? "true" : "false", static_cast<unsigned long>(time_get_utc()));
    }
  }

  mqtt_loop();
  web_server_loop();
  if (ENABLE_HA_DISCOVERY && !g_receiver_discovery_sent) {
    g_receiver_discovery_sent = mqtt_publish_discovery(g_device_id);
  }
  publish_faults_if_needed(g_device_id, g_receiver_faults, g_receiver_faults_published,
                           g_receiver_last_error, g_receiver_last_error_published, g_receiver_last_error_utc, g_receiver_last_error_ms);
  display_set_receiver_status(g_ap_mode, wifi_is_connected() ? wifi_get_ssid().c_str() : "AP", mqtt_is_connected());
  display_tick();
  watchdog_kick();
}

void loop() {
#ifdef ENABLE_TEST_MODE
  if (g_role == DeviceRole::Sender) {
    test_sender_loop(g_short_id, g_device_id);
    display_tick();
    watchdog_kick();
    delay(50);
  } else {
    test_receiver_loop(g_sender_statuses, NUM_SENDERS, g_short_id);
    mqtt_loop();
    web_server_loop();
    display_set_receiver_status(g_ap_mode, wifi_is_connected() ? wifi_get_ssid().c_str() : "AP", mqtt_is_connected());
    display_tick();
    watchdog_kick();
    delay(50);
  }
  return;
#endif

  if (g_role == DeviceRole::Sender) {
    sender_loop();
  } else {
    receiver_loop();
  }
}
