import 'package:uuid/uuid.dart';
import 'dart:io';
import 'package:tugas_akhir_supabase/screens/community/models/message.dart';

class GroupMessage extends Message {
  final String groupId;
  final bool isLocalImage;
  final File? localImageFile;

  GroupMessage({
    required super.id,
    required super.content,
    required super.senderEmail,
    required super.senderUsername,
    required super.senderUserId,
    super.imageUrl,
    required super.createdAt,
    super.replyToId,
    super.replyToContent,
    super.replyToSenderEmail,
    super.replyToSenderUsername, // Add this parameter
    super.avatarUrl,
    super.isRead = false,
    required this.groupId,
    this.isLocalImage = false,
    this.localImageFile,
  });

  factory GroupMessage.fromMap(Map<String, dynamic> map) {
    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 GroupMessage(
      id: messageId,
      content: map['content'] as String? ?? '',
      senderEmail: map['sender_email'] as String? ?? '',
      senderUsername: map['sender_username'] as String? ?? '',
      senderUserId: map['sender_user_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?, // Add this field
      avatarUrl: map['avatar_url'] as String?,
      groupId: map['group_id'] as String? ?? '',
      isLocalImage: map['isLocalImage'] as bool? ?? false,
      localImageFile: map['localImageFile'] as File?,
    );
  }

  // Create a GroupMessage from a Message
  factory GroupMessage.fromMessage(Message message, String groupId) {
    return GroupMessage(
      id: message.id,
      content: message.content,
      senderEmail: message.senderEmail,
      senderUsername: message.senderUsername,
      senderUserId: message.senderUserId,
      imageUrl: message.imageUrl,
      createdAt: message.createdAt,
      replyToId: message.replyToId,
      replyToContent: message.replyToContent,
      replyToSenderEmail: message.replyToSenderEmail,
      replyToSenderUsername: message.replyToSenderUsername, // Add this field
      avatarUrl: message.avatarUrl,
      isRead: message.isRead,
      groupId: groupId,
    );
  }

  // Create a deep copy of a GroupMessage
  factory GroupMessage.copy(GroupMessage message) {
    return GroupMessage(
      id: message.id,
      content: message.content,
      senderEmail: message.senderEmail,
      senderUsername: message.senderUsername,
      senderUserId: message.senderUserId,
      imageUrl: message.imageUrl,
      createdAt: message.createdAt,
      replyToId: message.replyToId,
      replyToContent: message.replyToContent,
      replyToSenderEmail: message.replyToSenderEmail,
      replyToSenderUsername: message.replyToSenderUsername, // Add this field
      avatarUrl: message.avatarUrl,
      isRead: message.isRead,
      groupId: message.groupId,
      isLocalImage: message.isLocalImage,
      localImageFile: message.localImageFile,
    );
  }

  @override
  Map<String, dynamic> toMap() {
    final map = super.toMap();
    map['group_id'] = groupId;
    return map;
  }

  GroupMessage copyWith({
    String? id,
    String? content,
    String? senderEmail,
    String? senderUsername,
    String? senderUserId,
    DateTime? createdAt,
    String? imageUrl,
    String? replyToId,
    String? replyToContent,
    String? replyToSenderEmail,
    String? replyToSenderUsername, // Add this parameter
    String? avatarUrl,
    bool? isRead,
    String? groupId,
    bool? isLocalImage,
    File? localImageFile,
  }) {
    return GroupMessage(
      id: id ?? this.id,
      content: content ?? this.content,
      senderEmail: senderEmail ?? this.senderEmail,
      senderUsername: senderUsername ?? this.senderUsername,
      senderUserId: senderUserId ?? this.senderUserId,
      createdAt: createdAt ?? this.createdAt,
      imageUrl: imageUrl ?? this.imageUrl,
      replyToId: replyToId ?? this.replyToId,
      replyToContent: replyToContent ?? this.replyToContent,
      replyToSenderEmail: replyToSenderEmail ?? this.replyToSenderEmail,
      replyToSenderUsername:
          replyToSenderUsername ?? this.replyToSenderUsername, // Add this field
      avatarUrl: avatarUrl ?? this.avatarUrl,
      isRead: isRead ?? this.isRead,
      groupId: groupId ?? this.groupId,
      isLocalImage: isLocalImage ?? this.isLocalImage,
      localImageFile: localImageFile ?? this.localImageFile,
    );
  }
}
