import 'package:equatable/equatable.dart';

/// Entité de domaine représentant un message de chat.
///
/// Un message est échangé entre deux utilisateurs dans une conversation.
class ChatMessage extends Equatable {
  const ChatMessage({
    required this.id,
    required this.conversationId,
    required this.senderId,
    required this.senderFirstName,
    required this.senderLastName,
    required this.senderProfileImageUrl,
    required this.content,
    required this.timestamp,
    required this.isRead,
    this.isDelivered = false,
    this.attachmentUrl,
    this.attachmentType,
  });

  final String id;
  final String conversationId;
  final String senderId;
  final String senderFirstName;
  final String senderLastName;
  final String? senderProfileImageUrl;
  final String content;
  final DateTime timestamp;
  final bool isRead;
  final bool isDelivered;
  final String? attachmentUrl; // URL d'une image/fichier joint
  final AttachmentType? attachmentType;

  /// Nom complet de l'expéditeur.
  String get senderFullName => '$senderFirstName $senderLastName';

  /// Indique si le message a une pièce jointe.
  bool get hasAttachment => attachmentUrl != null && attachmentType != null;

  @override
  List<Object?> get props => [
        id,
        conversationId,
        senderId,
        senderFirstName,
        senderLastName,
        senderProfileImageUrl,
        content,
        timestamp,
        isRead,
        isDelivered,
        attachmentUrl,
        attachmentType,
      ];

  /// Crée une copie de ce message avec des valeurs modifiées.
  ChatMessage copyWith({
    String? id,
    String? conversationId,
    String? senderId,
    String? senderFirstName,
    String? senderLastName,
    String? senderProfileImageUrl,
    String? content,
    DateTime? timestamp,
    bool? isRead,
    bool? isDelivered,
    String? attachmentUrl,
    AttachmentType? attachmentType,
  }) {
    return ChatMessage(
      id: id ?? this.id,
      conversationId: conversationId ?? this.conversationId,
      senderId: senderId ?? this.senderId,
      senderFirstName: senderFirstName ?? this.senderFirstName,
      senderLastName: senderLastName ?? this.senderLastName,
      senderProfileImageUrl: senderProfileImageUrl ?? this.senderProfileImageUrl,
      content: content ?? this.content,
      timestamp: timestamp ?? this.timestamp,
      isRead: isRead ?? this.isRead,
      isDelivered: isDelivered ?? this.isDelivered,
      attachmentUrl: attachmentUrl ?? this.attachmentUrl,
      attachmentType: attachmentType ?? this.attachmentType,
    );
  }
}

/// Type de pièce jointe.
enum AttachmentType {
  image,
  video,
  audio,
  file,
}

/// Extensions pour faciliter l'utilisation.
extension AttachmentTypeExtension on AttachmentType {
  String get displayName {
    switch (this) {
      case AttachmentType.image:
        return 'Image';
      case AttachmentType.video:
        return 'Vidéo';
      case AttachmentType.audio:
        return 'Audio';
      case AttachmentType.file:
        return 'Fichier';
    }
  }

  String get icon {
    switch (this) {
      case AttachmentType.image:
        return '🖼️';
      case AttachmentType.video:
        return '🎥';
      case AttachmentType.audio:
        return '🎵';
      case AttachmentType.file:
        return '📄';
    }
  }
}
