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

part 'participant_model.g.dart';

/// Modèle de participant pour le système de chat
///
/// Ce modèle représente un participant à une conversation

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

  @HiveField(1)
  final String conversationId;

  @HiveField(2)
  final String? userId;

  @HiveField(3)
  final String? anonymousId;

  @HiveField(4)
  final String role;

  @HiveField(5)
  final DateTime joinedAt;

  @HiveField(6)
  final String? lastReadMessageId;

  @HiveField(7)
  final bool viaTarget;

  @HiveField(8)
  final bool? canReply;

  ParticipantModel({
    required this.id,
    required this.conversationId,
    this.userId,
    this.anonymousId,
    required this.role,
    required this.joinedAt,
    this.lastReadMessageId,
    this.viaTarget = false,
    this.canReply,
  });

  /// Crée une instance depuis le JSON
  factory ParticipantModel.fromJson(Map<String, dynamic> json) {
    return ParticipantModel(
      id: json['id'] as String,
      conversationId: json['conversation_id'] as String,
      userId: json['user_id'] as String?,
      anonymousId: json['anonymous_id'] as String?,
      role: json['role'] as String,
      joinedAt: DateTime.parse(json['joined_at'] as String),
      lastReadMessageId: json['last_read_message_id'] as String?,
      viaTarget: json['via_target'] as bool? ?? false,
      canReply: json['can_reply'] as bool?,
    );
  }

  /// Convertit l'instance en JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'conversation_id': conversationId,
      'user_id': userId,
      'anonymous_id': anonymousId,
      'role': role,
      'joined_at': joinedAt.toIso8601String(),
      'last_read_message_id': lastReadMessageId,
      'via_target': viaTarget,
      'can_reply': canReply,
    };
  }

  /// Crée une copie modifiée de l'instance
  ParticipantModel copyWith({
    String? id,
    String? conversationId,
    String? userId,
    String? anonymousId,
    String? role,
    DateTime? joinedAt,
    String? lastReadMessageId,
    bool? viaTarget,
    bool? canReply,
  }) {
    return ParticipantModel(
      id: id ?? this.id,
      conversationId: conversationId ?? this.conversationId,
      userId: userId ?? this.userId,
      anonymousId: anonymousId ?? this.anonymousId,
      role: role ?? this.role,
      joinedAt: joinedAt ?? this.joinedAt,
      lastReadMessageId: lastReadMessageId ?? this.lastReadMessageId,
      viaTarget: viaTarget ?? this.viaTarget,
      canReply: canReply ?? this.canReply,
    );
  }

  @override
  List<Object?> get props => [
        id,
        conversationId,
        userId,
        anonymousId,
        role,
        joinedAt,
        lastReadMessageId,
        viaTarget,
        canReply,
      ];
}
