import 'package:uuid/uuid.dart';

class Message {
  final String id;
  final String content;
  final String senderEmail;
  final String senderUsername;
  final String senderUserId;
  final String? imageUrl;
  final DateTime createdAt;
  final String? replyToId;
  final String? replyToContent;
  final String? replyToSenderEmail;
  final String? replyToSenderUsername;
  final String? avatarUrl;
  final bool isRead;

  Message({
    required this.id,
    required this.content,
    required this.senderEmail,
    required this.senderUsername,
    required this.senderUserId,
    this.imageUrl,
    required this.createdAt,
    this.replyToId,
    this.replyToContent,
    this.replyToSenderEmail,
    this.replyToSenderUsername,
    this.avatarUrl,
    this.isRead = false,
  });

  // Create a copy of this message
  factory Message.copy(Message source) {
    return Message(
      id: source.id,
      content: source.content,
      senderEmail: source.senderEmail,
      senderUsername: source.senderUsername,
      senderUserId: source.senderUserId,
      imageUrl: source.imageUrl,
      createdAt: source.createdAt,
      replyToId: source.replyToId,
      replyToContent: source.replyToContent,
      replyToSenderEmail: source.replyToSenderEmail,
      replyToSenderUsername: source.replyToSenderUsername,
      avatarUrl: source.avatarUrl,
      isRead: source.isRead,
    );
  }

  // Create from database Map
  factory Message.fromMap(
    Map<String, dynamic> map, {
    required String? senderEmail,
    required String? senderUsername,
    required String? avatarUrl,
  }) {
    final createdAtStr = map['created_at'] as String?;
    final DateTime createdAt =
        createdAtStr != null ? DateTime.parse(createdAtStr) : DateTime.now();

    // Gunakan UUID yang valid untuk ID
    String messageId;
    if (map['id'] != null && map['id'] is String) {
      messageId = map['id'] as String;
    } else {
      // Generate UUID yang valid jika tidak ada
      messageId = const Uuid().v4();
    }

    return Message(
      id: messageId,
      content: map['content'] as String? ?? '',
      senderEmail: senderEmail ?? '',
      senderUsername: senderUsername ?? '',
      senderUserId: map['sender_id'] as String? ?? '',
      imageUrl: map['image_url'] as String?,
      createdAt: createdAt,
      replyToId: map['reply_to_id'] as String?,
      replyToContent: map['reply_to_content'] as String?,
      replyToSenderEmail: map['reply_to_sender_email'] as String?,
      replyToSenderUsername: map['reply_to_sender_username'] as String?,
      avatarUrl: avatarUrl,
    );
  }

  // Convert to database Map
  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'content': content,
      'sender_id': senderUserId,
      'sender_email': senderEmail,
      'created_at': createdAt.toIso8601String(),
      if (imageUrl != null && imageUrl!.isNotEmpty) 'image_url': imageUrl,
      if (replyToId != null && replyToId!.isNotEmpty) 'reply_to_id': replyToId,
      if (replyToContent != null && replyToContent!.isNotEmpty)
        'reply_to_content': replyToContent,
      if (replyToSenderEmail != null && replyToSenderEmail!.isNotEmpty)
        'reply_to_sender_email': replyToSenderEmail,
      if (replyToSenderUsername != null && replyToSenderUsername!.isNotEmpty)
        'reply_to_sender_username': replyToSenderUsername,
    };
  }

  // Get a short preview of this message content (for reply previews)
  String get contentPreview {
    if (content.isEmpty && imageUrl != null) {
      return '[Gambar]';
    } else if (content.isEmpty) {
      return '[Kosong]';
    } else if (content.length > 50) {
      return '${content.substring(0, 50)}...';
    } else {
      return content;
    }
  }
}
