#ifndef FFPS_H
#define FFPS_H

#include <cstddef>
#include "config.h"

// FFPS — Federated False-Positive Suppression
//
// Protocol:
// 1. Sensor detects candidate (class != noise, confidence > threshold)
// 2. Broadcasts CANDIDATE packet on mesh
// 3. Other sensors hearing the candidate compare against their own recent
//    observations and reply with CORROBORATE if they agree
// 4. Originator waits up to FFPS_CORROBORATION_WINDOW_MS
// 5. If >= FFPS_MIN_CORROBORATORS respond: PROMOTE (set FLAG_PROMOTED, publish DETECTION)
// 6. Otherwise: SUPPRESS (set FLAG_SUPPRESSED, log locally, do not publish)

enum FFPSState : uint8_t {
    FFPS_IDLE,
    FFPS_WAITING_CORROBORATION,
    FFPS_PROMOTED,
    FFPS_SUPPRESSED
};

// Initialize FFPS subsystem
void ffps_init(uint32_t sensor_id);

// Submit a candidate detection for corroboration.
// This broadcasts a CANDIDATE packet and starts the corroboration window.
// Returns true if the broadcast was sent.
bool ffps_submit_candidate(const DetectionEvent& candidate);

// Call this from the main loop to process incoming CANDIDATE/CORROBORATE packets
// and advance the corroboration timer.
// Returns the current FFPS state (PROMOTED / SUPPRESSED / WAITING / IDLE).
FFPSState ffps_tick();

// Handle an incoming mesh packet (called when mesh_receive gets data).
// Inspects msg_type and processes CANDIDATE or CORROBORATE.
void ffps_handle_packet(const uint8_t* data, size_t len, float rssi);

// Get the promoted detection event (valid only when ffps_tick returns FFPS_PROMOTED).
const DetectionEvent& ffps_get_promoted_event();

// Get corroboration count for the current/last candidate
uint8_t ffps_get_corroboration_count();

// Reset state machine to IDLE (after processing a promoted/suppressed result)
void ffps_reset();

#endif // FFPS_H
