import 'package:isar/isar.dart';
import '../../domain/entities/message.dart';

part 'message_model.g.dart';

@collection
class MessageModel {
  Id isarId = Isar.autoIncrement;

  @Index(unique: true, replace: true)
  late String id;

  @Index()
  late String chatId;
  
  late String senderId;
  late String content;
  late String messageType;
  late int sequenceId;
  
  @Index()
  DateTime? createdAt;
  
  DateTime? updatedAt;
  
  @Enumerated(EnumType.name)
  MessageStatus status = MessageStatus.delivered;
  
  // For simplicity, store media as a JSON string or ignore for now
  String? mediaJson;

  Message toEntity() {
    return Message(
      id: id,
      chatId: chatId,
      senderId: senderId,
      content: content,
      messageType: messageType,
      sequenceId: sequenceId,
      createdAt: createdAt,
      updatedAt: updatedAt,
      status: status,
    );
  }

  static MessageModel fromEntity(Message message) {
    return MessageModel()
      ..id = message.id
      ..chatId = message.chatId
      ..senderId = message.senderId
      ..content = message.content
      ..messageType = message.messageType
      ..sequenceId = message.sequenceId
      ..createdAt = message.createdAt
      ..updatedAt = message.updatedAt
      ..status = message.status;
  }
}
