import 'package:flutter_secure_storage/flutter_secure_storage.dart';

class LocalStore {
  // Secure storage instance
  final _storage = const FlutterSecureStorage();

  /// --------------------------------------------------------
  /// 🔐 SAVE LOGIN / SIGNUP DATA
  /// --------------------------------------------------------
  Future<void> saveLoginData(Map<String, dynamic> data) async {
    final token = data['access_token'] ?? '';
    final role = data['role'] ?? '';

    // Keep existing FCM token (do not overwrite)
    String? existingFcmToken = await _storage.read(key: 'fcm_token');

    // Save token & role
    await _storage.write(key: 'access_token', value: token.toString());
    await _storage.write(key: 'role', value: role.toString());

    // ⭐ Save general_chat_id if available
    if (data['general_chat_id'] != null) {
      await _storage.write(
        key: 'general_chat_id',
        value: data['general_chat_id'].toString(),
      );
    }

    // ⭐ Save USER ID if available
    if (data['user'] != null && data['user']['id'] != null) {
      await _storage.write(
        key: 'user_id',
        value: data['user']['id'].toString(),
      );
    }

    // ⭐ If root level ID exists (not in your JSON but future-safe)
    if (data['id'] != null) {
      await _storage.write(key: 'main_id', value: data['id'].toString());
    }

    // ⭐ Save/Persist FCM token safely
    if (existingFcmToken != null && existingFcmToken.isNotEmpty) {
      // Keep old FCM
      await _storage.write(key: 'fcm_token', value: existingFcmToken);
    } else if (data['fcm_token'] != null &&
        data['fcm_token'].toString().isNotEmpty) {
      // Save new FCM token
      await _storage.write(
        key: 'fcm_token',
        value: data['fcm_token'].toString(),
      );
    }
  }

  /// --------------------------------------------------------
  /// 🔥 SAVE FCM TOKEN ALONE
  /// --------------------------------------------------------
  Future<void> saveFcmToken(String token) async {
    await _storage.write(key: 'fcm_token', value: token);
  }

  /// --------------------------------------------------------
  /// 🔍 GETTERS
  /// --------------------------------------------------------
  Future<String?> getToken() async {
    return await _storage.read(key: 'access_token');
  }

  Future<String?> getRole() async {
    return await _storage.read(key: 'role');
  }

  Future<String?> getGeneralChatId() async {
    return await _storage.read(key: 'general_chat_id');
  }

  Future<String?> getUserId() async {
    return await _storage.read(key: 'user_id');
  }

  Future<String?> getMainId() async {
    return await _storage.read(key: 'main_id');
  }

  /// --------------------------------------------------------
  /// 🧹 CLEAR ALL (OPTIONAL KEEP FCM TOKEN)
  /// --------------------------------------------------------
  Future<void> clearAll({bool keepFcm = true}) async {
    if (keepFcm) {
      // Preserve FCM token
      String? fcmToken = await _storage.read(key: 'fcm_token');
      await _storage.deleteAll();
      if (fcmToken != null) {
        await _storage.write(key: 'fcm_token', value: fcmToken);
      }
    } else {
      // Full wipe
      await _storage.deleteAll();
    }
  }
}
