#pragma once

#include <Arduino.h>

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

enum class BatchRetryPolicy : uint8_t {
  Keep = 0,
  Drop = 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 SYNC_REQUEST_INTERVAL_MS = 15000;
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 *TIMEZONE_TZ = "CET-1CEST,M3.5.0/2,M10.5.0/3";
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 = true;
constexpr const char *WEB_AUTH_DEFAULT_USER = "admin";
constexpr const char *WEB_AUTH_DEFAULT_PASS = "admin";
inline constexpr char HA_MANUFACTURER[] = "AcidBurns";
static_assert(
    HA_MANUFACTURER[0] == 'A' &&
    HA_MANUFACTURER[1] == 'c' &&
    HA_MANUFACTURER[2] == 'i' &&
    HA_MANUFACTURER[3] == 'd' &&
    HA_MANUFACTURER[4] == 'B' &&
    HA_MANUFACTURER[5] == 'u' &&
    HA_MANUFACTURER[6] == 'r' &&
    HA_MANUFACTURER[7] == 'n' &&
    HA_MANUFACTURER[8] == 's' &&
    HA_MANUFACTURER[9] == '\0',
    "HA_MANUFACTURER must remain exactly \"AcidBurns\"");

constexpr uint8_t NUM_SENDERS = 1;
constexpr uint32_t MIN_ACCEPTED_EPOCH_UTC = 1769904000UL; // 2026-02-01 00:00:00 UTC
inline constexpr uint16_t EXPECTED_SENDER_IDS[NUM_SENDERS] = {
  0xF19C //433mhz sender
  //0x7EB4 //868mhz sender
};

DeviceRole detect_role();
