import 'package:hive_ce/hive.dart';

/// Central app settings storage using Hive
///
/// This box stores all app-level settings including:
/// - Theme settings (seed color, theme mode)
/// - Language preferences
/// - Notification settings
/// - User preferences
///
/// See APP_SETTINGS.md for complete documentation.
class AppSettingsBox {
  AppSettingsBox._();

  static const String boxName = 'app_settings';

  // ==================== Keys ====================

  // Theme Settings
  static const String seedColorId = 'seed_color_id';
  static const String themeMode = 'theme_mode';

  // Language Settings
  static const String languageCode = 'language_code';

  // Notification Settings
  static const String notificationsEnabled = 'notifications_enabled';
  static const String orderNotifications = 'order_notifications';
  static const String promotionNotifications = 'promotion_notifications';
  static const String chatNotifications = 'chat_notifications';

  // User Preferences
  static const String onboardingCompleted = 'onboarding_completed';
  static const String biometricEnabled = 'biometric_enabled';
  static const String rememberLogin = 'remember_login';

  // App State
  static const String lastSyncTime = 'last_sync_time';
  static const String appVersion = 'app_version';
  static const String firstLaunchDate = 'first_launch_date';

  // ==================== Box Instance ====================

  static Box<dynamic>? _box;

  /// Get the app settings box instance
  static Box<dynamic> get box {
    if (_box == null || !_box!.isOpen) {
      throw StateError(
        'AppSettingsBox not initialized. Call AppSettingsBox.init() first.',
      );
    }
    return _box!;
  }

  /// Initialize the app settings box - call before runApp()
  static Future<void> init() async {
    _box = await Hive.openBox<dynamic>(boxName);
  }

  /// Close the box
  static Future<void> close() async {
    await _box?.close();
    _box = null;
  }

  // ==================== Generic Getters/Setters ====================

  /// Get a value from the box
  static T? get<T>(String key, {T? defaultValue}) {
    return box.get(key, defaultValue: defaultValue) as T?;
  }

  /// Set a value in the box
  static Future<void> set<T>(String key, T value) async {
    await box.put(key, value);
  }

  /// Remove a value from the box
  static Future<void> remove(String key) async {
    await box.delete(key);
  }

  /// Check if a key exists
  static bool has(String key) {
    return box.containsKey(key);
  }

  /// Clear all settings
  static Future<void> clear() async {
    await box.clear();
  }

  // ==================== Theme Helpers ====================

  /// Get seed color ID
  static String getSeedColorId() {
    return get<String>(seedColorId, defaultValue: 'blue') ?? 'blue';
  }

  /// Set seed color ID
  static Future<void> setSeedColorId(String colorId) async {
    await set(seedColorId, colorId);
  }

  /// Get theme mode index (0=system, 1=light, 2=dark)
  static int getThemeModeIndex() {
    return get<int>(themeMode, defaultValue: 0) ?? 0;
  }

  /// Set theme mode index
  static Future<void> setThemeModeIndex(int index) async {
    await set(themeMode, index);
  }

  // ==================== Language Helpers ====================

  /// Get language code (vi, en)
  static String getLanguageCode() {
    return get<String>(languageCode, defaultValue: 'vi') ?? 'vi';
  }

  /// Set language code
  static Future<void> setLanguageCode(String code) async {
    await set(languageCode, code);
  }

  // ==================== Notification Helpers ====================

  /// Check if notifications are enabled
  static bool areNotificationsEnabled() {
    return get<bool>(notificationsEnabled, defaultValue: true) ?? true;
  }

  /// Set notifications enabled
  static Future<void> setNotificationsEnabled(bool enabled) async {
    await set(notificationsEnabled, enabled);
  }

  // ==================== User Preference Helpers ====================

  /// Check if onboarding is completed
  static bool isOnboardingCompleted() {
    return get<bool>(onboardingCompleted, defaultValue: false) ?? false;
  }

  /// Set onboarding completed
  static Future<void> setOnboardingCompleted(bool completed) async {
    await set(onboardingCompleted, completed);
  }

  /// Check if biometric is enabled
  static bool isBiometricEnabled() {
    return get<bool>(biometricEnabled, defaultValue: false) ?? false;
  }

  /// Set biometric enabled
  static Future<void> setBiometricEnabled(bool enabled) async {
    await set(biometricEnabled, enabled);
  }
}
