class LastMessageModel {
  final String body;
  final int timestamp;
  final bool fromMe;
  final bool hasMedia;

  LastMessageModel({
    required this.body,
    required this.timestamp,
    required this.fromMe,
    required this.hasMedia,
  });

  factory LastMessageModel.fromJson(Map<String, dynamic> json) {
    return LastMessageModel(
      body: json['body'] ?? '',
      timestamp: json['timestamp'] ?? 0,
      fromMe: json['fromMe'] ?? false,
      hasMedia: json['hasMedia'] ?? false,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'body': body,
      'timestamp': timestamp,
      'fromMe': fromMe,
      'hasMedia': hasMedia,
    };
  }
}

class ConversationModel {
  final String id;
  final String name;
  final bool isGroup;
  final int unreadCount;
  final String? avatar;
  final LastMessageModel? lastMessage;
  final int timestamp;
  final bool pinned;
  final bool isMuted;

  ConversationModel({
    required this.id,
    required this.name,
    required this.isGroup,
    required this.unreadCount,
    this.avatar,
    this.lastMessage,
    required this.timestamp,
    required this.pinned,
    required this.isMuted,
  });

  factory ConversationModel.fromJson(Map<String, dynamic> json) {
    return ConversationModel(
      id: json['id'] ?? '',
      name: json['name'] ?? '',
      isGroup: json['isGroup'] ?? false,
      unreadCount: json['unreadCount'] ?? 0,
      avatar: json['avatar'],
      lastMessage: json['lastMessage'] != null
          ? LastMessageModel.fromJson(json['lastMessage'])
          : null,
      timestamp: json['timestamp'] ?? 0,
      pinned: json['pinned'] ?? false,
      isMuted: json['isMuted'] ?? false,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'isGroup': isGroup,
      'unreadCount': unreadCount,
      'avatar': avatar,
      'lastMessage': lastMessage?.toJson(),
      'timestamp': timestamp,
      'pinned': pinned,
      'isMuted': isMuted,
    };
  }

  ConversationModel copyWith({
    String? id,
    String? name,
    bool? isGroup,
    int? unreadCount,
    String? avatar,
    LastMessageModel? lastMessage,
    int? timestamp,
    bool? pinned,
    bool? isMuted,
  }) {
    return ConversationModel(
      id: id ?? this.id,
      name: name ?? this.name,
      isGroup: isGroup ?? this.isGroup,
      unreadCount: unreadCount ?? this.unreadCount,
      avatar: avatar ?? this.avatar,
      lastMessage: lastMessage ?? this.lastMessage,
      timestamp: timestamp ?? this.timestamp,
      pinned: pinned ?? this.pinned,
      isMuted: isMuted ?? this.isMuted,
    );
  }
}
