/// Tests unitaires pour GetContributionById use case
library get_contribution_by_id_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_by_id.dart';
import 'package:unionflow_mobile_apps/features/contributions/data/models/contribution_model.dart';

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

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

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

  group('GetContributionById Use Case', () {
    const tContributionId = 'cont123';
    final tContribution = ContributionModel(
      id: tContributionId,
      membreId: 'membre1',
      membreNom: 'Dupont',
      membrePrenom: 'Jean',
      montant: 5000.0,
      montantPaye: 5000.0,
      dateEcheance: DateTime(2024, 12, 31),
      datePaiement: DateTime(2024, 11, 15),
      annee: 2024,
      type: ContributionType.annuelle,
      statut: ContributionStatus.payee,
      methodePaiement: PaymentMethod.waveMoney,
      numeroPaiement: 'WAVE123456',
    );

    test('should return contribution by id', () async {
      // Arrange
      when(mockRepository.getCotisationById(tContributionId))
          .thenAnswer((_) async => tContribution);

      // Act
      final result = await useCase(tContributionId);

      // Assert
      expect(result, equals(tContribution));
      expect(result.id, equals(tContributionId));
      expect(result.statut, equals(ContributionStatus.payee));
      verify(mockRepository.getCotisationById(tContributionId));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should return contribution with payment details', () async {
      // Arrange
      when(mockRepository.getCotisationById(tContributionId))
          .thenAnswer((_) async => tContribution);

      // Act
      final result = await useCase(tContributionId);

      // Assert
      expect(result.montantPaye, equals(5000.0));
      expect(result.methodePaiement, equals(PaymentMethod.waveMoney));
      expect(result.numeroPaiement, isNotNull);
    });

    test('should return unpaid contribution', () async {
      // Arrange
      final unpaidContribution = ContributionModel(
        id: 'cont456',
        membreId: 'membre2',
        montant: 10000.0,
        dateEcheance: DateTime(2025, 1, 31),
        annee: 2025,
        type: ContributionType.mensuelle,
        statut: ContributionStatus.enRetard,
      );
      when(mockRepository.getCotisationById('cont456'))
          .thenAnswer((_) async => unpaidContribution);

      // Act
      final result = await useCase('cont456');

      // Assert
      expect(result.statut, equals(ContributionStatus.enRetard));
      expect(result.montantPaye, isNull);
      expect(result.datePaiement, isNull);
    });

    test('should throw exception when contribution not found', () async {
      // Arrange
      when(mockRepository.getCotisationById(any))
          .thenThrow(Exception('Contribution non trouvée'));

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