#pragma once

#include <Arduino.h>

enum class DeviceRole : uint8_t {
  Sender = 0,
  Receiver = 1
};

enum class PayloadType : uint8_t {
  MeterData = 0,
  TestCode = 1,
  TimeSync = 2,
  MeterBatch = 3,
  Ack = 4
};

enum class BatchRetryPolicy : uint8_t {
  Keep = 0,
  Drop = 1
};

constexpr uint8_t PROTOCOL_VERSION = 1;

// Pin definitions
constexpr uint8_t PIN_LORA_SCK = 5;
constexpr uint8_t PIN_LORA_MISO = 19;
constexpr uint8_t PIN_LORA_MOSI = 27;
constexpr uint8_t PIN_LORA_NSS = 18;
constexpr uint8_t PIN_LORA_RST = 23;
constexpr uint8_t PIN_LORA_DIO0 = 26;

constexpr uint8_t PIN_OLED_SDA = 21;
constexpr uint8_t PIN_OLED_SCL = 22;
constexpr uint8_t PIN_OLED_RST = 16;
constexpr uint8_t OLED_I2C_ADDR = 0x3C;
constexpr uint8_t OLED_WIDTH = 128;
constexpr uint8_t OLED_HEIGHT = 64;

constexpr uint8_t PIN_BAT_ADC = 35;

constexpr uint8_t PIN_ROLE = 14;
constexpr uint8_t PIN_OLED_CTRL = 13;

constexpr uint8_t PIN_METER_RX = 34;

// LoRa settings
#ifndef LORA_FREQUENCY_HZ
#define LORA_FREQUENCY_HZ 433E6
#endif
constexpr long LORA_FREQUENCY = LORA_FREQUENCY_HZ;
constexpr uint8_t LORA_SPREADING_FACTOR = 12;
constexpr long LORA_BANDWIDTH = 125E3;
constexpr uint8_t LORA_CODING_RATE = 5;
constexpr uint8_t LORA_SYNC_WORD = 0x34;
constexpr uint8_t LORA_PREAMBLE_LEN = 8;

// Timing
constexpr uint32_t SENDER_WAKE_INTERVAL_SEC = 30;
constexpr uint32_t TIME_SYNC_INTERVAL_SEC = 60;
constexpr uint32_t TIME_SYNC_SLOW_INTERVAL_SEC = 3600;
constexpr uint32_t TIME_SYNC_FAST_WINDOW_MS = 10UL * 60UL * 1000UL;
constexpr uint32_t SENDER_TIMESYNC_WINDOW_MS = 300;
constexpr uint32_t SENDER_TIMESYNC_CHECK_SEC_FAST = 60;
constexpr uint32_t SENDER_TIMESYNC_CHECK_SEC_SLOW = 3600;
constexpr uint32_t TIME_SYNC_DRIFT_THRESHOLD_SEC = 10;
constexpr uint32_t TIME_SYNC_BURST_INTERVAL_MS = 10000;
constexpr uint32_t TIME_SYNC_BURST_DURATION_MS = 10UL * 60UL * 1000UL;
constexpr uint32_t TIME_SYNC_ERROR_TIMEOUT_MS = 2UL * 24UL * 60UL * 60UL * 1000UL;
constexpr bool ENABLE_DS3231 = true;
constexpr uint32_t OLED_PAGE_INTERVAL_MS = 4000;
constexpr uint32_t OLED_AUTO_OFF_MS = 10UL * 60UL * 1000UL;
constexpr uint32_t SENDER_OLED_READ_MS = 10000;
constexpr uint32_t METER_SAMPLE_INTERVAL_MS = 1000;
constexpr uint32_t METER_SEND_INTERVAL_MS = 30000;
constexpr uint32_t BATTERY_SAMPLE_INTERVAL_MS = 60000;
constexpr float BATTERY_CAL = 1.083f;
constexpr uint32_t BATCH_ACK_TIMEOUT_MS = 3000;
constexpr uint8_t ACK_REPEAT_COUNT = 3;
constexpr uint32_t ACK_REPEAT_DELAY_MS = 200;
constexpr uint8_t BATCH_MAX_RETRIES = 2;
constexpr uint8_t METER_BATCH_MAX_SAMPLES = 30;
constexpr uint8_t BATCH_QUEUE_DEPTH = 10;
constexpr BatchRetryPolicy BATCH_RETRY_POLICY = BatchRetryPolicy::Keep;
constexpr uint32_t WATCHDOG_TIMEOUT_SEC = 120;
constexpr bool ENABLE_HA_DISCOVERY = true;
#ifndef SERIAL_DEBUG_MODE_FLAG
#define SERIAL_DEBUG_MODE_FLAG 0
#endif
constexpr bool SERIAL_DEBUG_MODE = SERIAL_DEBUG_MODE_FLAG != 0;
constexpr bool SERIAL_DEBUG_DUMP_JSON = false;
constexpr bool LORA_SEND_BYPASS = false;
constexpr bool ENABLE_SD_LOGGING = true;
constexpr uint8_t PIN_SD_CS = 13;
constexpr uint8_t PIN_SD_MOSI = 15;
constexpr uint8_t PIN_SD_MISO = 2;
constexpr uint8_t PIN_SD_SCK = 14;
constexpr uint16_t SD_HISTORY_MAX_DAYS = 30;
constexpr uint16_t SD_HISTORY_MIN_RES_MIN = 1;
constexpr uint16_t SD_HISTORY_MAX_BINS = 4000;
constexpr uint16_t SD_HISTORY_TIME_BUDGET_MS = 10;
constexpr const char *AP_SSID_PREFIX = "DD3-Bridge-";
constexpr const char *AP_PASSWORD = "changeme123";
constexpr bool WEB_AUTH_REQUIRE_STA = true;
constexpr bool WEB_AUTH_REQUIRE_AP = false;
constexpr const char *WEB_AUTH_DEFAULT_USER = "admin";
constexpr const char *WEB_AUTH_DEFAULT_PASS = "admin";

constexpr uint8_t NUM_SENDERS = 1;
inline constexpr uint16_t EXPECTED_SENDER_IDS[NUM_SENDERS] = {
  0xF19C //433mhz sender
  //0x7EB4 //868mhz sender
};

DeviceRole detect_role();
