class EventModel {
  final String id;
  final String title;
  final String description;
  final String startDate;
  final String location;
  final String category;
  final String link;
  final String? imageUrl;
  final String creatorEmail;
  final List<dynamic> participants;
  final String status;
  final int reactionsCount;
  final int commentsCount;
  final int sharesCount;

  EventModel({
    required this.id,
    required this.title,
    required this.description,
    required this.startDate,
    required this.location,
    required this.category,
    required this.link,
    this.imageUrl,
    required this.creatorEmail,
    required this.participants,
    required this.status,
    required this.reactionsCount,
    required this.commentsCount,
    required this.sharesCount,
  });

  factory EventModel.fromJson(Map<String, dynamic> json) {
    print('[LOG] Création de l\'EventModel depuis JSON');

    // Utiliser les valeurs par défaut si une clé est absente
    final String id = json['id'] ?? 'ID Inconnu';
    final String title = json['title'] ?? 'Titre Inconnu';
    final String description = json['description'] ?? 'Description Inconnue';
    final String startDate = json['startDate'] ?? 'Date de début Inconnue';
    final String location = json['location'] ?? 'Localisation Inconnue';
    final String category = json['category'] ?? 'Catégorie Inconnue';
    final String link = json['link'] ?? 'Lien Inconnu';
    final String? imageUrl = json['imageUrl'];
    final String creatorEmail = json['creatorEmail'] ?? 'Email Inconnu';
    final List<dynamic> participants = json['participants'] ?? [];
    final String status = json['status'] ?? 'ouvert';
    final int reactionsCount = json['reactionsCount'] ?? 0;
    final int commentsCount = json['commentsCount'] ?? 0;
    final int sharesCount = json['sharesCount'] ?? 0;

    print('[LOG] Champs extraits depuis JSON :');
    print('  - ID: $id');
    print('  - Titre: $title');
    print('  - Description: $description');
    print('  - Date de début: $startDate');
    print('  - Localisation: $location');
    print('  - Catégorie: $category');
    print('  - Lien: $link');
    print('  - URL de l\'image: ${imageUrl ?? "Aucune"}');
    print('  - Email du créateur: $creatorEmail');
    print('  - Participants: ${participants.length} participants');
    print('  - Statut: $status');
    print('  - Nombre de réactions: $reactionsCount');
    print('  - Nombre de commentaires: $commentsCount');
    print('  - Nombre de partages: $sharesCount');

    return EventModel(
      id: id,
      title: title,
      description: description,
      startDate: startDate,
      location: location,
      category: category,
      link: link,
      imageUrl: imageUrl,
      creatorEmail: creatorEmail,
      participants: participants,
      status: status,
      reactionsCount: reactionsCount,
      commentsCount: commentsCount,
      sharesCount: sharesCount,
    );
  }

  Map<String, dynamic> toJson() {
    print('[LOG] Conversion de EventModel en JSON');
    return {
      'id': id,
      'title': title,
      'description': description,
      'startDate': startDate,
      'location': location,
      'category': category,
      'link': link,
      'imageUrl': imageUrl,
      'creatorEmail': creatorEmail,
      'participants': participants,
      'status': status,
      'reactionsCount': reactionsCount,
      'commentsCount': commentsCount,
      'sharesCount': sharesCount,
    };
  }
}
