import 'package:cloud_firestore/cloud_firestore.dart'; class Message { final String id; final String text; final DateTime timestamp; final String senderId; final String senderName; final String groupId; final Map reactions; // userId -> emoji final DateTime? editedAt; final bool isEdited; Message({ this.id = '', required this.text, required this.timestamp, required this.senderId, required this.senderName, required this.groupId, this.reactions = const {}, this.editedAt, this.isEdited = false, }); factory Message.fromFirestore(DocumentSnapshot doc) { final data = doc.data() as Map; final timestamp = data['timestamp'] as Timestamp?; final editedAtTimestamp = data['editedAt'] as Timestamp?; final reactionsData = data['reactions'] as Map?; return Message( id: doc.id, text: data['text'] ?? '', timestamp: timestamp?.toDate() ?? DateTime.now(), senderId: data['senderId'] ?? '', senderName: data['senderName'] ?? 'Anonyme', groupId: data['groupId'] ?? '', reactions: reactionsData?.map((key, value) => MapEntry(key, value.toString())) ?? {}, editedAt: editedAtTimestamp?.toDate(), isEdited: data['isEdited'] ?? false, ); } Map toFirestore() { return { 'text': text, 'senderId': senderId, 'senderName': senderName, 'timestamp': Timestamp.fromDate(timestamp), 'groupId': groupId, 'reactions': reactions, 'editedAt': editedAt != null ? Timestamp.fromDate(editedAt!) : null, 'isEdited': isEdited, }; } Message copyWith({ String? id, String? text, DateTime? timestamp, String? senderId, String? senderName, String? groupId, Map? reactions, DateTime? editedAt, bool? isEdited, }) { return Message( id: id ?? this.id, text: text ?? this.text, timestamp: timestamp ?? this.timestamp, senderId: senderId ?? this.senderId, senderName: senderName ?? this.senderName, groupId: groupId ?? this.groupId, reactions: reactions ?? this.reactions, editedAt: editedAt ?? this.editedAt, isEdited: isEdited ?? this.isEdited, ); } }