/// Entité métier Template de Message
///
/// Templates réutilisables pour notifications et broadcasts
library message_template;

import 'package:equatable/equatable.dart';

/// Catégorie de template
enum TemplateCategory {
  /// Événements
  events,

  /// Finances
  finances,

  /// Adhésions
  membership,

  /// Solidarité
  solidarity,

  /// Système
  system,

  /// Personnalisé
  custom,
}

/// Variables dynamiques dans les templates
class TemplateVariable {
  final String name;
  final String description;
  final String placeholder;
  final bool required;

  const TemplateVariable({
    required this.name,
    required this.description,
    required this.placeholder,
    this.required = true,
  });
}

/// Entité Template de Message
class MessageTemplate extends Equatable {
  final String id;
  final String name;
  final String description;
  final TemplateCategory category;
  final String subject;
  final String body;
  final List<TemplateVariable> variables;
  final String? organizationId;
  final String createdBy;
  final DateTime createdAt;
  final DateTime? updatedAt;
  final bool isActive;
  final bool isSystem;
  final int usageCount;
  final Map<String, dynamic>? metadata;

  const MessageTemplate({
    required this.id,
    required this.name,
    required this.description,
    required this.category,
    required this.subject,
    required this.body,
    this.variables = const [],
    this.organizationId,
    required this.createdBy,
    required this.createdAt,
    this.updatedAt,
    this.isActive = true,
    this.isSystem = false,
    this.usageCount = 0,
    this.metadata,
  });

  /// Vérifie si le template est éditable (pas système)
  bool get isEditable => !isSystem;

  /// Génère un message à partir du template avec des valeurs
  String generateMessage(Map<String, String> values) {
    String result = body;

    for (final variable in variables) {
      final value = values[variable.name];
      if (value != null) {
        result = result.replaceAll('{{${variable.name}}}', value);
      } else if (variable.required) {
        throw ArgumentError('Variable requise manquante: ${variable.name}');
      }
    }

    return result;
  }

  /// Copie avec modifications
  MessageTemplate copyWith({
    String? id,
    String? name,
    String? description,
    TemplateCategory? category,
    String? subject,
    String? body,
    List<TemplateVariable>? variables,
    String? organizationId,
    String? createdBy,
    DateTime? createdAt,
    DateTime? updatedAt,
    bool? isActive,
    bool? isSystem,
    int? usageCount,
    Map<String, dynamic>? metadata,
  }) {
    return MessageTemplate(
      id: id ?? this.id,
      name: name ?? this.name,
      description: description ?? this.description,
      category: category ?? this.category,
      subject: subject ?? this.subject,
      body: body ?? this.body,
      variables: variables ?? this.variables,
      organizationId: organizationId ?? this.organizationId,
      createdBy: createdBy ?? this.createdBy,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      isActive: isActive ?? this.isActive,
      isSystem: isSystem ?? this.isSystem,
      usageCount: usageCount ?? this.usageCount,
      metadata: metadata ?? this.metadata,
    );
  }

  @override
  List<Object?> get props => [
    id,
    name,
    description,
    category,
    subject,
    body,
    variables,
    organizationId,
    createdBy,
    createdAt,
    updatedAt,
    isActive,
    isSystem,
    usageCount,
    metadata,
  ];
}
