#ifndef BLE_H
#define BLE_H

#include "eis.h"

/* Commands: Cue → ESP32 (0x1x, 0x2x) */
#define CMD_SET_SWEEP     0x10
#define CMD_SET_RTIA      0x11
#define CMD_SET_RCAL      0x12
#define CMD_START_SWEEP   0x13
#define CMD_GET_CONFIG    0x14
#define CMD_SET_ELECTRODE 0x15
#define CMD_START_LSV     0x20
#define CMD_START_AMP     0x21
#define CMD_STOP_AMP      0x22

#define CMD_GET_TEMP      0x17
#define CMD_START_CL      0x23
#define CMD_START_PH      0x24
#define CMD_START_CLEAN   0x25
#define CMD_START_REFS    0x30
#define CMD_GET_REFS      0x31
#define CMD_CLEAR_REFS    0x32

/* Responses: ESP32 → Cue (0x0x) */
#define RSP_SWEEP_START 0x01
#define RSP_DATA_POINT  0x02
#define RSP_SWEEP_END   0x03
#define RSP_CONFIG      0x04
#define RSP_LSV_START   0x05
#define RSP_LSV_POINT   0x06
#define RSP_LSV_END     0x07
#define RSP_AMP_START   0x08
#define RSP_AMP_POINT   0x09
#define RSP_AMP_END     0x0A
#define RSP_CL_START    0x0B
#define RSP_CL_POINT    0x0C
#define RSP_CL_RESULT   0x0D
#define RSP_CL_END      0x0E
#define RSP_PH_RESULT   0x0F
#define RSP_TEMP        0x10
#define RSP_REF_FRAME   0x20
#define RSP_REF_LP_RANGE 0x21
#define RSP_REFS_DONE   0x22
#define RSP_REF_STATUS  0x23

typedef struct {
    uint8_t type;
    union {
        struct { float freq_start, freq_stop; uint16_t ppd; } sweep;
        uint8_t rtia;
        uint8_t rcal;
        uint8_t electrode;
        struct { float v_start, v_stop, scan_rate; uint8_t lp_rtia; } lsv;
        struct { float v_hold, interval_ms, duration_s; uint8_t lp_rtia; } amp;
        struct { float v_cond, t_cond_ms, v_free, v_total, t_dep_ms, t_meas_ms; uint8_t lp_rtia; } cl;
        struct { float stabilize_s; } ph;
        struct { float v_mv; float duration_s; } clean;
    };
} BleCommand;

int  ble_init(void);
int  ble_is_connected(void);
void ble_wait_for_connection(void);

/* blocking receive from command queue */
int ble_recv_command(BleCommand *cmd, uint32_t timeout_ms);

/* outbound: EIS */
int ble_send_sweep_start(uint32_t num_points, float freq_start, float freq_stop);
int ble_send_eis_point(uint16_t index, const EISPoint *pt);
int ble_send_sweep_end(void);
int ble_send_config(const EISConfig *cfg);

/* outbound: LSV */
int ble_send_lsv_start(uint32_t num_points, float v_start, float v_stop);
int ble_send_lsv_point(uint16_t index, float v_mv, float i_ua);
int ble_send_lsv_end(void);

/* outbound: Amperometry */
int ble_send_amp_start(float v_hold);
int ble_send_amp_point(uint16_t index, float t_ms, float i_ua);
int ble_send_amp_end(void);

/* outbound: Chlorine */
int ble_send_cl_start(uint32_t num_points);
int ble_send_cl_point(uint16_t index, float t_ms, float i_ua, uint8_t phase);
int ble_send_cl_result(float i_free_ua, float i_total_ua);
int ble_send_cl_end(void);

/* outbound: pH */
int ble_send_ph_result(float v_ocp_mv, float ph, float temp_c);

/* outbound: temperature */
int ble_send_temp(float temp_c);

/* outbound: reference collection */
int ble_send_ref_frame(uint8_t mode, uint8_t rtia_idx);
int ble_send_ref_lp_range(uint8_t mode, uint8_t low_idx, uint8_t high_idx);
int ble_send_refs_done(void);
int ble_send_ref_status(uint8_t has_refs);

#endif
