import '../../domain/entities/conversation.dart';

/// Modèle de données pour les conversations (Data Transfer Object).
class ConversationModel {
  ConversationModel({
    required this.id,
    required this.participantId,
    required this.participantFirstName,
    required this.participantLastName,
    this.participantProfileImageUrl,
    this.lastMessage,
    this.lastMessageTimestamp,
    required this.unreadCount,
    this.isTyping = false,
  });

  /// Factory pour créer un [ConversationModel] à partir d'un JSON.
  factory ConversationModel.fromJson(Map<String, dynamic> json) {
    return ConversationModel(
      id: _parseId(json, 'id', ''),
      participantId: _parseId(json, 'participantId', ''),
      participantFirstName: _parseString(json, 'participantFirstName', ''),
      participantLastName: _parseString(json, 'participantLastName', ''),
      participantProfileImageUrl: json['participantProfileImageUrl'] as String?,
      lastMessage: json['lastMessage'] as String?,
      lastMessageTimestamp: json['lastMessageTimestamp'] != null
          ? DateTime.parse(json['lastMessageTimestamp'] as String)
          : null,
      unreadCount: json['unreadCount'] as int? ?? 0,
      isTyping: json['isTyping'] as bool? ?? false,
    );
  }

  /// Factory pour créer un [ConversationModel] à partir d'une entité.
  factory ConversationModel.fromEntity(Conversation conversation) {
    return ConversationModel(
      id: conversation.id,
      participantId: conversation.participantId,
      participantFirstName: conversation.participantFirstName,
      participantLastName: conversation.participantLastName,
      participantProfileImageUrl: conversation.participantProfileImageUrl,
      lastMessage: conversation.lastMessage,
      lastMessageTimestamp: conversation.lastMessageTimestamp,
      unreadCount: conversation.unreadCount,
      isTyping: conversation.isTyping,
    );
  }

  final String id;
  final String participantId;
  final String participantFirstName;
  final String participantLastName;
  final String? participantProfileImageUrl;
  final String? lastMessage;
  final DateTime? lastMessageTimestamp;
  final int unreadCount;
  final bool isTyping;

  /// Convertit ce modèle en JSON pour l'envoi vers l'API.
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'participantId': participantId,
      'participantFirstName': participantFirstName,
      'participantLastName': participantLastName,
      if (participantProfileImageUrl != null) 'participantProfileImageUrl': participantProfileImageUrl,
      if (lastMessage != null) 'lastMessage': lastMessage,
      if (lastMessageTimestamp != null) 'lastMessageTimestamp': lastMessageTimestamp!.toIso8601String(),
      'unreadCount': unreadCount,
      'isTyping': isTyping,
    };
  }

  /// Convertit ce modèle vers une entité de domaine [Conversation].
  Conversation toEntity() {
    return Conversation(
      id: id,
      participantId: participantId,
      participantFirstName: participantFirstName,
      participantLastName: participantLastName,
      participantProfileImageUrl: participantProfileImageUrl,
      lastMessage: lastMessage,
      lastMessageTimestamp: lastMessageTimestamp,
      unreadCount: unreadCount,
      isTyping: isTyping,
    );
  }

  // Méthodes de parsing
  static String _parseString(Map<String, dynamic> json, String key, String defaultValue) {
    return json[key] as String? ?? defaultValue;
  }

  static String _parseId(Map<String, dynamic> json, String key, String defaultValue) {
    final value = json[key];
    if (value == null) return defaultValue;
    return value.toString();
  }
}
