/// Entité métier Audit Log Financier
///
/// Représente une entrée d'audit trail pour traçabilité financière complète
library financial_audit_log;

import 'package:equatable/equatable.dart';

/// Type d'opération auditée
enum AuditOperation {
  /// Création
  create,

  /// Lecture/consultation
  read,

  /// Mise à jour
  update,

  /// Suppression
  delete,

  /// Approbation
  approve,

  /// Rejet
  reject,

  /// Validation
  validate,

  /// Annulation
  cancel,

  /// Export
  export,
}

/// Type d'entité auditée
enum AuditEntityType {
  /// Contribution/cotisation
  contribution,

  /// Transaction épargne
  savingsTransaction,

  /// Approbation
  approval,

  /// Budget
  budget,

  /// Dépense solidarité
  solidarity,

  /// Paiement
  payment,

  /// Autre
  other,
}

/// Niveau de sévérité
enum AuditSeverity {
  /// Information
  info,

  /// Avertissement
  warning,

  /// Erreur
  error,

  /// Critique (anomalie détectée)
  critical,
}

/// Entité Audit Log Financier
class FinancialAuditLog extends Equatable {
  final String id;
  final AuditOperation operation;
  final AuditEntityType entityType;
  final String entityId;
  final String userId;
  final String userName;
  final String userRole;
  final String? organizationId;
  final AuditSeverity severity;
  final String description;
  final Map<String, dynamic>? dataBefore;
  final Map<String, dynamic>? dataAfter;
  final double? amountBefore;
  final double? amountAfter;
  final String? ipAddress;
  final String? userAgent;
  final DateTime timestamp;
  final Map<String, dynamic>? metadata;

  const FinancialAuditLog({
    required this.id,
    required this.operation,
    required this.entityType,
    required this.entityId,
    required this.userId,
    required this.userName,
    required this.userRole,
    this.organizationId,
    this.severity = AuditSeverity.info,
    required this.description,
    this.dataBefore,
    this.dataAfter,
    this.amountBefore,
    this.amountAfter,
    this.ipAddress,
    this.userAgent,
    required this.timestamp,
    this.metadata,
  });

  /// Vérifie si c'est une opération de modification de montant
  bool get isAmountChanged =>
      amountBefore != null &&
      amountAfter != null &&
      amountBefore != amountAfter;

  /// Écart de montant
  double? get amountDifference {
    if (amountBefore == null || amountAfter == null) return null;
    return amountAfter! - amountBefore!;
  }

  /// Vérifie si c'est une anomalie critique
  bool get isCritical => severity == AuditSeverity.critical;

  /// Vérifie si c'est une opération sensible
  bool get isSensitiveOperation =>
      operation == AuditOperation.delete ||
      operation == AuditOperation.approve ||
      operation == AuditOperation.reject ||
      operation == AuditOperation.cancel;

  @override
  List<Object?> get props => [
        id,
        operation,
        entityType,
        entityId,
        userId,
        userName,
        userRole,
        organizationId,
        severity,
        description,
        dataBefore,
        dataAfter,
        amountBefore,
        amountAfter,
        ipAddress,
        userAgent,
        timestamp,
        metadata,
      ];
}
