import 'package:hive/hive.dart';
import 'package:equatable/equatable.dart';

part 'message_model.g.dart';

/// Modèle de message pour le système de chat
///
/// Ce modèle représente un message échangé dans une conversation

@HiveType(typeId: 21)
class MessageModel extends HiveObject with EquatableMixin {
  @HiveField(0)
  final String id;

  @HiveField(1)
  final String conversationId;

  @HiveField(2)
  final String? senderId;

  @HiveField(3)
  final String senderType;

  @HiveField(4)
  final String content;

  @HiveField(5)
  final String contentType;

  @HiveField(6)
  final DateTime createdAt;

  @HiveField(7)
  final DateTime? deliveredAt;

  @HiveField(8)
  final DateTime? readAt;

  @HiveField(9)
  final String status;

  @HiveField(10)
  final bool isAnnouncement;

  MessageModel({
    required this.id,
    required this.conversationId,
    this.senderId,
    required this.senderType,
    required this.content,
    required this.contentType,
    required this.createdAt,
    this.deliveredAt,
    this.readAt,
    required this.status,
    this.isAnnouncement = false,
  });

  /// Crée une instance depuis le JSON
  factory MessageModel.fromJson(Map<String, dynamic> json) {
    return MessageModel(
      id: json['id'] as String,
      conversationId: json['conversation_id'] as String,
      senderId: json['sender_id'] as String?,
      senderType: json['sender_type'] as String,
      content: json['content'] as String,
      contentType: json['content_type'] as String,
      createdAt: DateTime.parse(json['created_at'] as String),
      deliveredAt: json['delivered_at'] != null
          ? DateTime.parse(json['delivered_at'] as String)
          : null,
      readAt: json['read_at'] != null
          ? DateTime.parse(json['read_at'] as String)
          : null,
      status: json['status'] as String,
      isAnnouncement: json['is_announcement'] as bool? ?? false,
    );
  }

  /// Convertit l'instance en JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'conversation_id': conversationId,
      'sender_id': senderId,
      'sender_type': senderType,
      'content': content,
      'content_type': contentType,
      'created_at': createdAt.toIso8601String(),
      'delivered_at': deliveredAt?.toIso8601String(),
      'read_at': readAt?.toIso8601String(),
      'status': status,
      'is_announcement': isAnnouncement,
    };
  }

  /// Crée une copie modifiée de l'instance
  MessageModel copyWith({
    String? id,
    String? conversationId,
    String? senderId,
    String? senderType,
    String? content,
    String? contentType,
    DateTime? createdAt,
    DateTime? deliveredAt,
    DateTime? readAt,
    String? status,
    bool? isAnnouncement,
  }) {
    return MessageModel(
      id: id ?? this.id,
      conversationId: conversationId ?? this.conversationId,
      senderId: senderId ?? this.senderId,
      senderType: senderType ?? this.senderType,
      content: content ?? this.content,
      contentType: contentType ?? this.contentType,
      createdAt: createdAt ?? this.createdAt,
      deliveredAt: deliveredAt ?? this.deliveredAt,
      readAt: readAt ?? this.readAt,
      status: status ?? this.status,
      isAnnouncement: isAnnouncement ?? this.isAnnouncement,
    );
  }

  @override
  List<Object?> get props => [
        id,
        conversationId,
        senderId,
        senderType,
        content,
        contentType,
        createdAt,
        deliveredAt,
        readAt,
        status,
        isAnnouncement,
      ];
}
