import 'dart:convert';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';

class SecureStorage {
  final FlutterSecureStorage _storage;

  SecureStorage() : _storage = const FlutterSecureStorage();

  Future<void> write(String key, dynamic value) async {
    final stringValue = value is String ? value : json.encode(value);
    await _storage.write(key: key, value: stringValue);
  }

  Future<dynamic> read(String key) async {
    final value = await _storage.read(key: key);
    if (value == null) return null;

    try {
      return json.decode(value);
    } catch (_) {
      return value;
    }
  }

  Future<void> delete(String key) async {
    await _storage.delete(key: key);
  }

  Future<void> deleteAll() async {
    await _storage.deleteAll();
  }
}
