/// Tests unitaires pour GetMemberStats use case
library get_member_stats_test;

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:unionflow_mobile_apps/features/members/domain/repositories/membre_repository.dart';
import 'package:unionflow_mobile_apps/features/members/domain/usecases/get_member_stats.dart';

@GenerateMocks([IMembreRepository])
import 'get_member_stats_test.mocks.dart';

void main() {
  late GetMemberStats useCase;
  late MockIMembreRepository mockRepository;

  setUp(() {
    mockRepository = MockIMembreRepository();
    useCase = GetMemberStats(mockRepository);
  });

  group('GetMemberStats Use Case', () {
    final tMemberStats = {
      'totalMembres': 250,
      'membresActifs': 180,
      'membresInactifs': 50,
      'membresSuspendus': 15,
      'membresEnAttente': 5,
      'repartitionGenre': {
        'hommes': 130,
        'femmes': 115,
        'autre': 5,
      },
      'nouveauxMembresMois': 12,
      'tauxActivation': 0.72,
      'agesMoyens': {
        'global': 42.5,
        'hommes': 44.2,
        'femmes': 40.8,
      },
    };

    test('should return comprehensive member statistics', () async {
      // Arrange
      when(mockRepository.getMembresStats())
          .thenAnswer((_) async => tMemberStats);

      // Act
      final result = await useCase();

      // Assert
      expect(result, equals(tMemberStats));
      expect(result['totalMembres'], equals(250));
      expect(result['membresActifs'], equals(180));
      expect(result['tauxActivation'], equals(0.72));
      verify(mockRepository.getMembresStats());
      verifyNoMoreInteractions(mockRepository);
    });

    test('should return gender distribution statistics', () async {
      // Arrange
      when(mockRepository.getMembresStats())
          .thenAnswer((_) async => tMemberStats);

      // Act
      final result = await useCase();

      // Assert
      final repartition = result['repartitionGenre'] as Map<String, dynamic>;
      expect(repartition['hommes'], equals(130));
      expect(repartition['femmes'], equals(115));
      expect(repartition['autre'], equals(5));
    });

    test('should return empty stats when no members exist', () async {
      // Arrange
      final emptyStats = {
        'totalMembres': 0,
        'membresActifs': 0,
        'membresInactifs': 0,
        'membresSuspendus': 0,
        'membresEnAttente': 0,
        'repartitionGenre': {
          'hommes': 0,
          'femmes': 0,
          'autre': 0,
        },
        'nouveauxMembresMois': 0,
        'tauxActivation': 0.0,
      };
      when(mockRepository.getMembresStats())
          .thenAnswer((_) async => emptyStats);

      // Act
      final result = await useCase();

      // Assert
      expect(result['totalMembres'], equals(0));
      expect(result['tauxActivation'], equals(0.0));
    });

    test('should throw exception when stats retrieval fails', () async {
      // Arrange
      when(mockRepository.getMembresStats())
          .thenThrow(Exception('Erreur serveur'));

      // Act & Assert
      expect(() => useCase(), throwsException);
    });
  });
}
