#ifndef DEBUG_H
#define DEBUG_H

// Debug output system - comment out DEBUG_MODE for production builds
#include <Arduino.h>

// PRODUCTION BUILD: Debug output disabled
// Uncomment the line below to re-enable debug output for development
// #define DEBUG_MODE
// #define DEBUG_VERBOSE

// Debug levels
#define DEBUG_LEVEL_NONE    0
#define DEBUG_LEVEL_ERROR   1
#define DEBUG_LEVEL_WARN    2
#define DEBUG_LEVEL_INFO    3
#define DEBUG_LEVEL_DEBUG   4
#define DEBUG_LEVEL_VERBOSE 5

#ifndef DEBUG_LEVEL
  #ifdef DEBUG_VERBOSE
    #define DEBUG_LEVEL DEBUG_LEVEL_VERBOSE
  #else
    #define DEBUG_LEVEL DEBUG_LEVEL_INFO
  #endif
#endif

#ifdef DEBUG_MODE

  #define DEBUG_PRINT(x)      Serial.print(x)
  #define DEBUG_PRINTLN(x)    Serial.println(x)
  #define DEBUG_PRINTF(...)   Serial.printf(__VA_ARGS__)
  
  #if DEBUG_LEVEL >= DEBUG_LEVEL_ERROR
    #define DEBUG_ERROR(x)    do { Serial.print("[ERROR] "); Serial.println(x); } while(0)
    #define DEBUG_ERRORF(...) do { Serial.print("[ERROR] "); Serial.printf(__VA_ARGS__); } while(0)
  #else
    #define DEBUG_ERROR(x)
    #define DEBUG_ERRORF(...)
  #endif
  
  #if DEBUG_LEVEL >= DEBUG_LEVEL_WARN
    #define DEBUG_WARN(x)     do { Serial.print("[WARN] "); Serial.println(x); } while(0)
    #define DEBUG_WARNF(...)  do { Serial.print("[WARN] "); Serial.printf(__VA_ARGS__); } while(0)
  #else
    #define DEBUG_WARN(x)
    #define DEBUG_WARNF(...)
  #endif
  
  #if DEBUG_LEVEL >= DEBUG_LEVEL_INFO
    #define DEBUG_INFO(x)     Serial.println(x)
    #define DEBUG_INFOF(...)  Serial.printf(__VA_ARGS__)
  #else
    #define DEBUG_INFO(x)
    #define DEBUG_INFOF(...)
  #endif
  
  #if DEBUG_LEVEL >= DEBUG_LEVEL_DEBUG
    #define DEBUG_DBG(x)      do { Serial.print("[DBG] "); Serial.println(x); } while(0)
    #define DEBUG_DBGF(...)   do { Serial.print("[DBG] "); Serial.printf(__VA_ARGS__); } while(0)
  #else
    #define DEBUG_DBG(x)
    #define DEBUG_DBGF(...)
  #endif
  
  #if DEBUG_LEVEL >= DEBUG_LEVEL_VERBOSE
    #define DEBUG_VERBOSE_PRINT(x)   Serial.print(x)
    #define DEBUG_VERBOSE_PRINTLN(x) Serial.println(x)
    #define DEBUG_VERBOSE_PRINTF(...) Serial.printf(__VA_ARGS__)
  #else
    #define DEBUG_VERBOSE_PRINT(x)
    #define DEBUG_VERBOSE_PRINTLN(x)
    #define DEBUG_VERBOSE_PRINTF(...)
  #endif
  
  #define DEBUG_SECTION_START() Serial.println(F("════════════════════════════════════"))
  #define DEBUG_SECTION_END()   Serial.println(F("════════════════════════════════════"))
  #define DEBUG_HEAP() Serial.printf("[Heap] Free: %d bytes (min: %d)\n", ESP.getFreeHeap(), ESP.getMinFreeHeap())
  #define DEBUG_TEMP() Serial.printf("[Temp] Chip: %.1f°C\n", temperatureRead())
  #define DEBUG_TIMING_START(name) unsigned long _timing_##name = millis()
  #define DEBUG_TIMING_END(name)   Serial.printf("[Timing] %s: %ldms\n", #name, millis() - _timing_##name)

#else

  #define DEBUG_PRINT(x)
  #define DEBUG_PRINTLN(x)
  #define DEBUG_PRINTF(...)
  #define DEBUG_ERROR(x)
  #define DEBUG_ERRORF(...)
  #define DEBUG_WARN(x)
  #define DEBUG_WARNF(...)
  #define DEBUG_INFO(x)
  #define DEBUG_INFOF(...)
  #define DEBUG_DBG(x)
  #define DEBUG_DBGF(...)
  #define DEBUG_VERBOSE_PRINT(x)
  #define DEBUG_VERBOSE_PRINTLN(x)
  #define DEBUG_VERBOSE_PRINTF(...)
  #define DEBUG_SECTION_START()
  #define DEBUG_SECTION_END()
  #define DEBUG_HEAP()
  #define DEBUG_TEMP()
  #define DEBUG_TIMING_START(name)
  #define DEBUG_TIMING_END(name)

#endif

#ifdef DEBUG_MODE
  #define DEBUG_PRINT_F(x)    Serial.print(F(x))
  #define DEBUG_PRINTLN_F(x)  Serial.println(F(x))
#else
  #define DEBUG_PRINT_F(x)
  #define DEBUG_PRINTLN_F(x)
#endif

#endif
