initialize method

Future<void> initialize()

Inicializa el storage con técnicas defensivas

Implementation

Future<void> initialize() async {
  if (_isInitialized) {
    ObslyLogger.debug('🔧 ObslyStorage already initialized');
    return;
  }

  ObslyLogger.log('🔧 ObslyStorage.initialize() starting...');

  try {
    ObslyLogger.debug('🔧 Attempting Hive initialization...');
    await _initializeHive();
    _useHive = true;
    ObslyLogger.log('✅ Storage initialized with Hive successfully');
    ObslyLogger.debug(
        '🔧 Hive boxes: events=${_eventsBox != null}, internalState=${_internalStateBox != null}, performance=${_performanceBox != null}');
  } catch (e) {
    ObslyLogger.warn('⚠️  Hive initialization failed, falling back to SharedPreferences: $e');
    try {
      await _initializeSharedPreferences();
      _useHive = false;
      ObslyLogger.log('✅ Storage initialized with SharedPreferences fallback');
      ObslyLogger.debug('🔧 SharedPreferences ready: ${_prefs != null}');
    } catch (e2) {
      ObslyLogger.error('❌ Both Hive and SharedPreferences initialization failed: $e2');
      ObslyLogger.warn('⚠️  Falling back to in-memory storage - data will not persist');
      _useHive = false;
      _prefs = null;
      // Don't throw - continue with in-memory fallback
    }
  }

  // Limpiar eventos antiguos en background
  _cleanupOldEvents();

  _isInitialized = true;
  ObslyLogger.log('✅ ObslyStorage initialization complete - useHive: $_useHive');
}