#pragma once

#include <Arduino.h>

// ------------------------- Hardware configuration -------------------------
#if CONFIG_IDF_TARGET_ESP32C3
constexpr bool TARGET_IS_C3 = true;
constexpr uint8_t GPIO_PWM = 3;
constexpr uint8_t GPIO_RX = 4;
constexpr uint8_t GPIO_BUTTON_MODE = 0;
constexpr uint8_t GPIO_BUTTON_START = 1;
constexpr uint8_t GPIO_SDA = 6;
constexpr uint8_t GPIO_SCL = 7;
#elif CONFIG_IDF_TARGET_ESP32S3
constexpr bool TARGET_IS_C3 = false;
constexpr uint8_t GPIO_PWM = 4;
constexpr uint8_t GPIO_RX = 5;
constexpr uint8_t GPIO_BUTTON_MODE = 6;
constexpr uint8_t GPIO_BUTTON_START = 7;
constexpr uint8_t GPIO_SDA = 8;
constexpr uint8_t GPIO_SCL = 9;
#else
#error "Only ESP32-C3 and ESP32-S3 are supported"
#endif

constexpr uint8_t OLED_ROTATION = 0;

constexpr uint8_t OLED_ADDRESS = 0x3C;
constexpr uint8_t ESPNOW_WIFI_CHANNEL = 6;
constexpr uint32_t SERIAL_BAUD = 115200;
constexpr bool SERIAL_ACTION_LOG = true;
constexpr bool SERIAL_LOG_TIMESTAMPS = true;
constexpr bool SERIAL_MINIMAL_LOG = true;

#define BUTTON_ACTIVE_LEVEL LOW
#define RX_SIGNAL_INVERTED false
#define PWM_SAFE_LEVEL LOW
#define PWM_SETTLE_CYCLES 5U

constexpr uint32_t BUTTON_DEBOUNCE_MS = 30;
constexpr uint32_t BUTTON_LONG_PRESS_MS = 500;
constexpr uint32_t BUTTON_REPEAT_DELAY_MS = 600;
constexpr uint32_t BUTTON_REPEAT_MS = 180;
constexpr uint32_t FACTORY_RESET_HOLD_MS = 1500;

constexpr uint32_t LINK_REPLY_TIMEOUT_MS = 1500;
constexpr uint8_t LINK_PACKET_RETRIES = 10;
constexpr uint32_t LINK_RETRY_INTERVAL_MS = 1000;
constexpr uint32_t DISCOVERY_RETRY_INTERVAL_MS = 20;
constexpr uint32_t LINK_HEARTBEAT_INTERVAL_MS = 500;
constexpr uint32_t LINK_HEARTBEAT_TIMEOUT_MS = 2500;
constexpr uint32_t FINAL_ACK_RETRY_INTERVAL_MS = 50;
constexpr uint8_t FINAL_ACK_RETRIES = 2;
constexpr uint8_t NO_SIGNAL_TIMEOUT_PERIODS = 8;
constexpr uint16_t RMT_MIN_RECEIVE_SYMBOLS = 48;
constexpr uint16_t RMT_MAX_RECEIVE_SYMBOLS = 512;
constexpr uint32_t RMT_TARGET_CHUNK_US = 5000;
constexpr uint8_t RMT_QUEUE_BLOCKS = 8;
constexpr uint16_t PERIOD_BATCH_SIZE = 128;
constexpr uint8_t MEASUREMENT_PROGRESS_STEPS = 10;
constexpr uint32_t OLED_PROGRESS_UPDATE_MS = 15;

constexpr uint32_t IDLE_POWER_SAVE_TIMEOUT_MS = 60000;
constexpr uint32_t IDLE_LIGHT_SLEEP_SLICE_US = 10000;
constexpr uint16_t SLAVE_LISTEN_INTERVAL_MS = 100;
constexpr uint16_t SLAVE_LISTEN_WINDOW_MS = 20;
static_assert(SLAVE_LISTEN_WINDOW_MS < SLAVE_LISTEN_INTERVAL_MS,
  "Slave listen window must be shorter than its interval");
// Conservative sustained validation rate calibrated from real C3 logs.
constexpr uint32_t RX_PROCESSING_PERIODS_PER_SECOND = 300000;

constexpr uint32_t C3_STRICT_MAX_HZ = 1000000;
constexpr uint32_t S3_STRICT_MAX_HZ = 1000000;
// RMT stores each HIGH/LOW duration in 15 bits. Select the fastest clock that
// still fits both levels of the current PWM signal: 20, 40 or 80 MHz.
constexpr uint32_t CAPTURE_RESOLUTION_OPTIONS_HZ[] = {20000000, 40000000, 80000000};
constexpr uint32_t RMT_MAX_LEVEL_TICKS = 32766;
// Arduino-ESP32 uses the 40 MHz crystal as the default LEDC clock on C3/S3.
// Keep this explicit so the resolution calculation never asks LEDC for an
// impossible frequency/resolution combination.
constexpr uint32_t LEDC_SOURCE_CLOCK_HZ = 40000000;
constexpr uint8_t LEDC_CHANNEL = 0;
constexpr uint8_t LEDC_MAX_BITS = 14;

// -------------------------- Menu value arrays -----------------------------
// START and END deliberately have separate, independently cycling menu lists.
// Every value is exactly achievable from the 40 MHz XTAL with an integer LEDC
// divider. The test itself walks TEST_FREQUENCIES_HZ between the selected
// endpoints, so there is no separately configurable step.
constexpr uint32_t START_FREQ_OPTIONS_HZ[] = {1000, 10000, 100000};
constexpr uint32_t END_FREQ_OPTIONS_HZ[] = {100000, 500000, 1000000};

// All achievable whole-number frequencies in the supported 1 kHz..1 MHz
// range, used for adjacent test stages rather than direct menu selection.
constexpr uint32_t TEST_FREQUENCIES_HZ[] = {
  1000, 2000, 5000, 10000, 25000, 50000,
  100000, 200000, 312500, 400000, 500000, 625000, 800000, 1000000
};
constexpr float ACCURACY_OPTIONS_PCT[] = {1.0f, 2.0f, 5.0f, 10.0f};
constexpr uint32_t TEST_TIME_OPTIONS_MS[] = {100, 250, 500, 1000, 2000, 5000};
constexpr uint8_t DUTY_OPTIONS_PCT[] = {10, 25, 50, 75, 90};

template <typename T, size_t N> constexpr size_t countOf(const T (&)[N]) { return N; }
