/// Entité métier Conversation
///
/// Représente une conversation (fil de messages) dans UnionFlow
library conversation;

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

/// Type de conversation
enum ConversationType {
  /// Conversation individuelle (1-1)
  individual,

  /// Conversation de groupe
  group,

  /// Canal broadcast (lecture seule pour la plupart)
  broadcast,

  /// Canal d'annonces organisation
  announcement,
}

/// Entité Conversation
class Conversation extends Equatable {
  final String id;
  final String name;
  final String? description;
  final ConversationType type;
  final List<String> participantIds;
  final String? organizationId;
  final Message? lastMessage;
  final int unreadCount;
  final bool isMuted;
  final bool isPinned;
  final bool isArchived;
  final DateTime createdAt;
  final DateTime? updatedAt;
  final String? avatarUrl;
  final Map<String, dynamic>? metadata;

  const Conversation({
    required this.id,
    required this.name,
    this.description,
    required this.type,
    required this.participantIds,
    this.organizationId,
    this.lastMessage,
    this.unreadCount = 0,
    this.isMuted = false,
    this.isPinned = false,
    this.isArchived = false,
    required this.createdAt,
    this.updatedAt,
    this.avatarUrl,
    this.metadata,
  });

  /// Vérifie si la conversation a des messages non lus
  bool get hasUnread => unreadCount > 0;

  /// Vérifie si c'est une conversation individuelle
  bool get isIndividual => type == ConversationType.individual;

  /// Vérifie si c'est un broadcast
  bool get isBroadcast => type == ConversationType.broadcast;

  /// Nombre de participants
  int get participantCount => participantIds.length;

  /// Copie avec modifications
  Conversation copyWith({
    String? id,
    String? name,
    String? description,
    ConversationType? type,
    List<String>? participantIds,
    String? organizationId,
    Message? lastMessage,
    int? unreadCount,
    bool? isMuted,
    bool? isPinned,
    bool? isArchived,
    DateTime? createdAt,
    DateTime? updatedAt,
    String? avatarUrl,
    Map<String, dynamic>? metadata,
  }) {
    return Conversation(
      id: id ?? this.id,
      name: name ?? this.name,
      description: description ?? this.description,
      type: type ?? this.type,
      participantIds: participantIds ?? this.participantIds,
      organizationId: organizationId ?? this.organizationId,
      lastMessage: lastMessage ?? this.lastMessage,
      unreadCount: unreadCount ?? this.unreadCount,
      isMuted: isMuted ?? this.isMuted,
      isPinned: isPinned ?? this.isPinned,
      isArchived: isArchived ?? this.isArchived,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      avatarUrl: avatarUrl ?? this.avatarUrl,
      metadata: metadata ?? this.metadata,
    );
  }

  @override
  List<Object?> get props => [
    id,
    name,
    description,
    type,
    participantIds,
    organizationId,
    lastMessage,
    unreadCount,
    isMuted,
    isPinned,
    isArchived,
    createdAt,
    updatedAt,
    avatarUrl,
    metadata,
  ];
}
