/// Tests unitaires pour GetContributionStats use case
library get_contribution_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/contributions/domain/repositories/contribution_repository.dart';
import 'package:unionflow_mobile_apps/features/contributions/domain/usecases/get_contribution_stats.dart';

@GenerateMocks([IContributionRepository])
import 'get_contribution_stats_test.mocks.dart';

void main() {
  late GetContributionStats useCase;
  late MockIContributionRepository mockRepository;

  setUp(() {
    mockRepository = MockIContributionRepository();
    useCase = GetContributionStats(mockRepository);
  });

  group('GetContributionStats Use Case', () {
    final tStats = {
      'montantDu': 60000.0,
      'totalPayeAnnee': 45000.0,
      'cotisationsEnAttente': 3,
      'prochaineEcheance': '2024-12-31T00:00:00.000Z',
      'tauxPaiement': 75.0,
      'nombreCotisations': 12,
      'montantMoyenCotisation': 5000.0,
    };

    test('should return contribution statistics', () async {
      // Arrange
      when(mockRepository.getMesCotisationsSynthese())
          .thenAnswer((_) async => tStats);

      // Act
      final result = await useCase();

      // Assert
      expect(result, equals(tStats));
      expect(result?['montantDu'], equals(60000.0));
      expect(result?['totalPayeAnnee'], equals(45000.0));
      expect(result?['tauxPaiement'], equals(75.0));
      verify(mockRepository.getMesCotisationsSynthese());
      verifyNoMoreInteractions(mockRepository);
    });

    test('should return stats with payment rate', () async {
      // Arrange
      when(mockRepository.getMesCotisationsSynthese())
          .thenAnswer((_) async => tStats);

      // Act
      final result = await useCase();

      // Assert
      expect(result?['tauxPaiement'], equals(75.0));
      expect(result?['cotisationsEnAttente'], equals(3));
    });

    test('should return stats with next deadline', () async {
      // Arrange
      when(mockRepository.getMesCotisationsSynthese())
          .thenAnswer((_) async => tStats);

      // Act
      final result = await useCase();

      // Assert
      expect(result?['prochaineEcheance'], isNotNull);
      expect(result?['prochaineEcheance'], contains('2024-12-31'));
    });

    test('should return null when no data available', () async {
      // Arrange
      when(mockRepository.getMesCotisationsSynthese())
          .thenAnswer((_) async => null);

      // Act
      final result = await useCase();

      // Assert
      expect(result, isNull);
      verify(mockRepository.getMesCotisationsSynthese());
    });
  });
}
