/**
 * Globale Debug-Utility für die gesamte Anwendung
 * Konfigurierbar über .env Variablen
 */

class DebugLogger {
  constructor() {
    // Debug-Konfiguration aus .env Datei
    this.isEnabled = process.env.REACT_APP_DEBUG_ENABLED === 'true';
    this.apiDebug = process.env.REACT_APP_DEBUG_API === 'true';
    this.routingDebug = process.env.REACT_APP_DEBUG_ROUTING === 'true';
    this.authDebug = process.env.REACT_APP_DEBUG_AUTH === 'true';
    
    // Fallback: In Development-Umgebung standardmäßig aktiviert
    if (process.env.NODE_ENV === 'development' && this.isEnabled === undefined) {
      this.isEnabled = true;
      this.apiDebug = true;
      this.routingDebug = true;
      this.authDebug = true;
    }
  }

  // Allgemeine Debug-Logs
  log(message, ...args) {
    if (this.isEnabled) {
      console.log(`🔧 [DEBUG]`, message, ...args);
    }
  }

  // API-bezogene Debug-Logs
  api(message, ...args) {
    if (this.isEnabled && this.apiDebug) {
      console.log(`📡 [API]`, message, ...args);
    }
  }

  // Routing-bezogene Debug-Logs
  routing(message, ...args) {
    if (this.isEnabled && this.routingDebug) {
      console.log(`🚀 [ROUTING]`, message, ...args);
    }
  }

  // Authentifizierungs-bezogene Debug-Logs
  auth(message, ...args) {
    if (this.isEnabled && this.authDebug) {
      console.log(`🔐 [AUTH]`, message, ...args);
    }
  }

  // Error-Logs (immer aktiviert, auch in Production für kritische Fehler)
  error(message, ...args) {
    console.error(`❌ [ERROR]`, message, ...args);
  }

  // Warning-Logs (immer aktiviert)
  warn(message, ...args) {
    console.warn(`⚠️ [WARN]`, message, ...args);
  }

  // Success-Logs (nur wenn Debug aktiviert)
  success(message, ...args) {
    if (this.isEnabled) {
      console.log(`✅ [SUCCESS]`, message, ...args);
    }
  }

  // Info-Logs (nur wenn Debug aktiviert)
  info(message, ...args) {
    if (this.isEnabled) {
      console.info(`ℹ️ [INFO]`, message, ...args);
    }
  }

  // Gruppen für zusammenhängende Logs
  group(title) {
    if (this.isEnabled) {
      console.group(`🔧 [DEBUG GROUP] ${title}`);
    }
  }

  groupEnd() {
    if (this.isEnabled) {
      console.groupEnd();
    }
  }

  // Zeitmessung für Performance-Debugging
  time(label) {
    if (this.isEnabled) {
      console.time(`⏱️ [TIMER] ${label}`);
    }
  }

  timeEnd(label) {
    if (this.isEnabled) {
      console.timeEnd(`⏱️ [TIMER] ${label}`);
    }
  }

  // Debug-Status anzeigen
  getStatus() {
    return {
      enabled: this.isEnabled,
      api: this.apiDebug,
      routing: this.routingDebug,
      auth: this.authDebug,
      environment: process.env.NODE_ENV
    };
  }
}

// Singleton-Instanz exportieren
const debugLogger = new DebugLogger();

// Zeige Debug-Status beim Import
if (debugLogger.isEnabled) {
  debugLogger.info('Debug Logger initialized:', debugLogger.getStatus());
}

export default debugLogger;
