/// Préférences de notifications pour un utilisateur
class NotificationPreferences {
  final bool emailEnabled;             // Recevoir emails
  final bool pushEnabled;              // Recevoir notifications push
  final bool inAppEnabled;             // Recevoir alertes in-app

  // Préférences par type d'alerte
  final bool eventsNotifications;      // Alertes événements
  final bool maintenanceNotifications; // Alertes maintenance
  final bool stockNotifications;       // Alertes stock
  final bool equipmentNotifications;   // Alertes équipement

  // Token FCM (pour push)
  final String? fcmToken;

  const NotificationPreferences({
    this.emailEnabled = true,  // ✓ Activé par défaut
    this.pushEnabled = false,
    this.inAppEnabled = true,
    this.eventsNotifications = true,
    this.maintenanceNotifications = true,
    this.stockNotifications = true,
    this.equipmentNotifications = true,
    this.fcmToken,
  });

  /// Valeurs par défaut pour un nouvel utilisateur
  factory NotificationPreferences.defaults() {
    return const NotificationPreferences(
      emailEnabled: true,  // ✓ Activé par défaut
      pushEnabled: false,
      inAppEnabled: true,
      eventsNotifications: true,
      maintenanceNotifications: true,
      stockNotifications: true,
      equipmentNotifications: true,
    );
  }

  factory NotificationPreferences.fromMap(Map<String, dynamic> map) {
    return NotificationPreferences(
      emailEnabled: map['emailEnabled'] ?? true,  // ✓ true par défaut
      pushEnabled: map['pushEnabled'] ?? false,
      inAppEnabled: map['inAppEnabled'] ?? true,
      eventsNotifications: map['eventsNotifications'] ?? true,
      maintenanceNotifications: map['maintenanceNotifications'] ?? true,
      stockNotifications: map['stockNotifications'] ?? true,
      equipmentNotifications: map['equipmentNotifications'] ?? true,
      fcmToken: map['fcmToken'],
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'emailEnabled': emailEnabled,
      'pushEnabled': pushEnabled,
      'inAppEnabled': inAppEnabled,
      'eventsNotifications': eventsNotifications,
      'maintenanceNotifications': maintenanceNotifications,
      'stockNotifications': stockNotifications,
      'equipmentNotifications': equipmentNotifications,
      if (fcmToken != null) 'fcmToken': fcmToken,
    };
  }

  NotificationPreferences copyWith({
    bool? emailEnabled,
    bool? pushEnabled,
    bool? inAppEnabled,
    bool? eventsNotifications,
    bool? maintenanceNotifications,
    bool? stockNotifications,
    bool? equipmentNotifications,
    String? fcmToken,
  }) {
    return NotificationPreferences(
      emailEnabled: emailEnabled ?? this.emailEnabled,
      pushEnabled: pushEnabled ?? this.pushEnabled,
      inAppEnabled: inAppEnabled ?? this.inAppEnabled,
      eventsNotifications: eventsNotifications ?? this.eventsNotifications,
      maintenanceNotifications: maintenanceNotifications ?? this.maintenanceNotifications,
      stockNotifications: stockNotifications ?? this.stockNotifications,
      equipmentNotifications: equipmentNotifications ?? this.equipmentNotifications,
      fcmToken: fcmToken ?? this.fcmToken,
    );
  }
}

