#ifndef FCB_H
#define FCB_H

#include <stdio.h>
#include <string.h>
#include <stdint.h>

// simple struct to hold sensor data for FCB storage
// struct FCB_sensor_data {
// 	/* Actual sensor data payload (sent via MQTT) */
// 	// struct __attribute__((packed)) {
// 		uint64_t sensor_ts;                // sensor timestamp (8 bytes)
// 		uint32_t message_counter;          // message counter (4 bytes)
// 		int16_t temp_deg_c;                // temperature * 100 (2 bytes)
// 		uint16_t humidity_percent;         // humidity * 100  (2 bytes)
// 		uint16_t pressure_kpa;             // pressure * 100 (2 bytes)
// 		uint8_t padding[2];                // Explicit padding to align struct size to 4 bytes (total size 84)
// 	// } payload;
// } __attribute__((packed));

struct FCB_sensor_data {
	/* Actual sensor data payload (sent via MQTT) */
	struct __attribute__((packed)) {
		uint32_t message_counter;          // message counter (4 bytes)
		uint64_t sensor_ts;                // sensor timestamp (8 bytes)
		int16_t temp_deg_c;                // temperature * 100 (2 bytes)
		uint16_t humidity_percent;         // humidity * 100  (2 bytes)
		uint16_t pressure_kpa;             // pressure * 100 (2 bytes)
		uint64_t pulse_counter;            // pulse counter (8 bytes)
		uint64_t accumulated_pulse_counter; // accumulated pulse counter (snapshot) (8 bytes)
		uint64_t accumulated_message_counter; // accumulated message counter (snapshot) (8 bytes)
		uint16_t battery_voltage_mv;       // battery voltage in mV (2 bytes)
		uint8_t reg_status;                // network registration status (1 byte)
		uint32_t plmn;                     // PLMN (converted from string) (4 bytes)
		uint16_t tac;                      // TAC (converted from hex string) (2 bytes)
		uint8_t act;                       // Access technology (1 byte)
		uint8_t band;                      // Band number (1 byte)
		uint32_t cell_id;                  // Cell ID (converted from hex string) (4 bytes)
		uint16_t phys_cell_id;             // Physical cell ID (2 bytes)
		uint16_t earfcn;                   // EARFCN (2 bytes)
		uint8_t rsrp;                      // RSRP (1 byte)
		uint8_t snr;                       // SNR (1 byte)
		int64_t app_run_time;              // application runtime (8 bytes)
		uint8_t app_version_major;         // application version major (1 byte)
		uint8_t app_version_minor;         // application version minor  (1 byte)
		uint8_t app_version_patch;         // application version patch (1 byte)
		uint32_t reset_reason;             // reset reason (4 bytes)
		uint8_t padding[6];                // Explicit padding to align struct size to 4 bytes (total size 84)
	} payload;
} __attribute__((packed));

int FCB_init();
int FCB_append_entry(const struct FCB_sensor_data *data, size_t len);
void FCB_read_sector(uint16_t sector_no);
void FCB_read_all_sectors(void);

#endif // FCB_H

