import 'package:hub/features/storage/index.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SharedPreferencesStorage implements BaseStorage {
  SharedPreferencesStorage._();

  static final SharedPreferencesStorage instance = SharedPreferencesStorage._();

  late final SharedPreferences _prefs;
  bool _isInitialized = false;

  @override
  Future<void> init() async {
    if (_isInitialized) return;
    _prefs = await SharedPreferences.getInstance();
    _isInitialized = true;
  }

  @override
  Future<String?> get(String key) async {
    return _prefs.get(key).toString();
  }

  @override
  Future<void> set<T>(String key, T value) async {
    await _prefs.setString(key, value.toString());
  }

  @override
  Future<void> delete(String key) async {
    await _prefs.remove(key);
  }

  @override
  Future<void> clearAll() async {
    await _prefs.clear();
  }
}
