#ifndef INTEROP_ROUTER_H
#define INTEROP_ROUTER_H

// ============================================================================
// interop_router.{h,cpp}
//
// Glue between the three interop protocols and the existing PDL mesh.
//
// Inbound:
//   meshtastic_handle_packet() / rnode_handle_packet() call
//   interop_router_enqueue_pdl() with a pre-formatted MSG_TYPE_INTEROP_RX
//   buffer. The router holds it in a small ring buffer until the radio
//   scheduler is back in PDL mode, then sends it via mesh_publish_raw().
//
// Outbound:
//   When the C2 (over LoRa command channel OR MQTT) issues an INTEROP_TX
//   verb, interop_router_dispatch_tx() chooses the protocol byte (1 = MT,
//   2 = RNode) and calls the right backend.
// ============================================================================

#include <cstdint>
#include <cstddef>

// Source-protocol values (byte [1] of MSG_TYPE_INTEROP_RX/TX wire payload).
#define INTEROP_PROTO_MESHTASTIC  1
#define INTEROP_PROTO_RNODE       2

// ---------------------------------------------------------------------------
// Lifecycle
// ---------------------------------------------------------------------------
void interop_router_init();

// Pump the queue — call from the main loop AFTER radio_sched_tick().
void interop_router_tick();

// Enqueue a pre-built MSG_TYPE_INTEROP_RX packet for delivery on the
// next PDL window. Drops silently if queue is full.
void interop_router_enqueue_pdl(const uint8_t* pkt, size_t len);

// Outbound dispatch: pkt[0]=proto, pkt[1..N] = protocol-specific payload.
// The exact format per protocol:
//   MT  : [0]=proto, [1..] = ASCII text (NUL-terminated within len)
//   RN  : [0]=proto, [1..32]=hex dest hash, [33]=plen, [34..] = LXMF bytes
bool interop_router_dispatch_tx(const uint8_t* pkt, size_t len);

// Stats
struct InteropStats {
    uint32_t enq_dropped_full;
    uint32_t enq_total;
    uint32_t flushed_to_pdl;
    uint32_t tx_dispatched;
    uint32_t tx_dropped;
};
void interop_router_get_stats(InteropStats* out);

#ifdef UNIT_TEST
extern "C" int interop_router_test_proto_select(uint8_t first_byte);
#endif

#endif // INTEROP_ROUTER_H
