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

part 'conversation_model.g.dart';

/// Modèle de conversation pour le système de chat
///
/// Ce modèle représente une conversation entre utilisateurs
/// Il supporte différents types de conversations :
/// - one_to_one : conversation privée entre 2 utilisateurs
/// - group : groupe de plusieurs utilisateurs
/// - anonymous : conversation avec un utilisateur anonyme
/// - broadcast : message diffusé à plusieurs utilisateurs
/// - announcement : annonce officielle

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

  @HiveField(1)
  final String type;

  @HiveField(2)
  final String? title;

  @HiveField(3)
  final DateTime createdAt;

  @HiveField(4)
  final DateTime updatedAt;

  @HiveField(5)
  final List<ParticipantModel> participants;

  @HiveField(6)
  final bool isSynced;

  @HiveField(7)
  final String replyPermission;

  @HiveField(8)
  final bool isPinned;

  @HiveField(9)
  final DateTime? expiryDate;

  ConversationModel({
    required this.id,
    required this.type,
    this.title,
    required this.createdAt,
    required this.updatedAt,
    required this.participants,
    this.isSynced = false,
    this.replyPermission = 'all',
    this.isPinned = false,
    this.expiryDate,
  });

  /// Crée une instance depuis le JSON
  factory ConversationModel.fromJson(Map<String, dynamic> json) {
    return ConversationModel(
      id: json['id'] as String,
      type: json['type'] as String,
      title: json['title'] as String?,
      createdAt: DateTime.parse(json['created_at'] as String),
      updatedAt: DateTime.parse(json['updated_at'] as String),
      participants: (json['participants'] as List?)
              ?.map((e) => ParticipantModel.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
      isSynced: json['is_synced'] as bool? ?? false,
      replyPermission: json['reply_permission'] as String? ?? 'all',
      isPinned: json['is_pinned'] as bool? ?? false,
      expiryDate: json['expiry_date'] != null
          ? DateTime.parse(json['expiry_date'] as String)
          : null,
    );
  }

  /// Convertit l'instance en JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'type': type,
      'title': title,
      'created_at': createdAt.toIso8601String(),
      'updated_at': updatedAt.toIso8601String(),
      'participants': participants.map((e) => e.toJson()).toList(),
      'is_synced': isSynced,
      'reply_permission': replyPermission,
      'is_pinned': isPinned,
      'expiry_date': expiryDate?.toIso8601String(),
    };
  }

  /// Crée une copie modifiée de l'instance
  ConversationModel copyWith({
    String? id,
    String? type,
    String? title,
    DateTime? createdAt,
    DateTime? updatedAt,
    List<ParticipantModel>? participants,
    bool? isSynced,
    String? replyPermission,
    bool? isPinned,
    DateTime? expiryDate,
  }) {
    return ConversationModel(
      id: id ?? this.id,
      type: type ?? this.type,
      title: title ?? this.title,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      participants: participants ?? this.participants,
      isSynced: isSynced ?? this.isSynced,
      replyPermission: replyPermission ?? this.replyPermission,
      isPinned: isPinned ?? this.isPinned,
      expiryDate: expiryDate ?? this.expiryDate,
    );
  }

  @override
  List<Object?> get props => [
        id,
        type,
        title,
        createdAt,
        updatedAt,
        participants,
        isSynced,
        replyPermission,
        isPinned,
        expiryDate,
      ];
}
