// =============================================================================
//  station/station_mgr.h -- Control authority state machine
// =============================================================================
//
//  Manages who has command authority over the autopilot.
//  Rules (from docs/concentrador_protocol.md):
//    - Station 01 (bridge) has override priority — TAKECMD is immediate.
//    - Other stations request command via REQCMD, wait for ACK or timeout (10s).
//    - Only the current commander can release (RELCMD) or deny requests (DENYCMD).
// =============================================================================

#pragma once

#include <cstdint>

namespace arconcentrador::station {

static constexpr uint8_t STATION_NONE   = 0x00;
static constexpr uint8_t STATION_BRIDGE = 0x01;  // highest priority

/// Initialise with no commander (STATION_NONE).
void station_init();

/// Return the current commander ID (0 = no commander).
uint8_t current_commander();

/// Process an incoming $PARP sentence already parsed into fields.
/// cmd is the command field (e.g. "REQCMD", "TAKECMD", "RELCMD", ...).
/// station_id is the sender's station ID (1-4).
/// out_broadcast is set to a full $PARP sentence to broadcast (or empty string).
void station_process(const char* cmd, uint8_t station_id,
                     char* out_broadcast, size_t out_len);

/// Call once per loop — handles the 10-second REQCMD timeout.
void station_tick();

}  // namespace arconcentrador::station
