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

part 'audience_target_model.g.dart';

/// Modèle de cible d'audience pour le système de chat
///
/// Ce modèle représente une cible d'audience pour les annonces et broadcasts
/// Il supporte maintenant le ciblage combiné avec les filtres de rôle et d'entité

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

  @HiveField(1)
  final String conversationId;

  @HiveField(2)
  final String targetType;

  @HiveField(3)
  final String? targetId;

  @HiveField(4)
  final DateTime createdAt;

  @HiveField(5)
  final String? roleFilter;

  @HiveField(6)
  final String? entityFilter;

  AudienceTargetModel({
    required this.id,
    required this.conversationId,
    required this.targetType,
    this.targetId,
    required this.createdAt,
    this.roleFilter,
    this.entityFilter,
  });

  /// Crée une instance depuis le JSON
  factory AudienceTargetModel.fromJson(Map<String, dynamic> json) {
    return AudienceTargetModel(
      id: json['id'] as String,
      conversationId: json['conversation_id'] as String,
      targetType: json['target_type'] as String,
      targetId: json['target_id'] as String?,
      createdAt: DateTime.parse(json['created_at'] as String),
      roleFilter: json['role_filter'] as String?,
      entityFilter: json['entity_filter'] as String?,
    );
  }

  /// Convertit l'instance en JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'conversation_id': conversationId,
      'target_type': targetType,
      'target_id': targetId,
      'created_at': createdAt.toIso8601String(),
      'role_filter': roleFilter,
      'entity_filter': entityFilter,
    };
  }

  /// Crée une copie modifiée de l'instance
  AudienceTargetModel copyWith({
    String? id,
    String? conversationId,
    String? targetType,
    String? targetId,
    DateTime? createdAt,
    String? roleFilter,
    String? entityFilter,
  }) {
    return AudienceTargetModel(
      id: id ?? this.id,
      conversationId: conversationId ?? this.conversationId,
      targetType: targetType ?? this.targetType,
      targetId: targetId ?? this.targetId,
      createdAt: createdAt ?? this.createdAt,
      roleFilter: roleFilter ?? this.roleFilter,
      entityFilter: entityFilter ?? this.entityFilter,
    );
  }

  /// Vérifie si l'utilisateur est ciblé par cette règle
  bool targetsUser({
    required String userId,
    required int userRole,
    required String userEntityId,
  }) {
    switch (targetType) {
      case 'all':
        return true;
      case 'role':
        if (roleFilter != null && roleFilter != 'all') {
          return userRole.toString() == roleFilter;
        }
        return true;
      case 'entity':
        if (entityFilter != null && entityFilter != 'all') {
          return userEntityId == entityFilter;
        }
        return true;
      case 'combined':
        bool matchesRole = true;
        bool matchesEntity = true;

        if (roleFilter != null && roleFilter != 'all') {
          matchesRole = userRole.toString() == roleFilter;
        }

        if (entityFilter != null && entityFilter != 'all') {
          matchesEntity = userEntityId == entityFilter;
        }

        return matchesRole && matchesEntity;
      default:
        return false;
    }
  }

  @override
  List<Object?> get props => [
        id,
        conversationId,
        targetType,
        targetId,
        createdAt,
        roleFilter,
        entityFilter,
      ];
}
