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

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

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

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

  group('UpdateContribution Use Case', () {
    const tContributionId = 'cont123';
    final tUpdatedContribution = ContributionModel(
      id: tContributionId,
      membreId: 'membre1',
      montant: 6000.0,
      dateEcheance: DateTime(2025, 12, 31),
      annee: 2025,
      type: ContributionType.annuelle,
      statut: ContributionStatus.nonPayee,
      description: 'Montant mis à jour',
    );

    test('should update contribution successfully', () async {
      // Arrange
      when(mockRepository.updateCotisation(tContributionId, tUpdatedContribution))
          .thenAnswer((_) async => tUpdatedContribution);

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

      // Assert
      expect(result, equals(tUpdatedContribution));
      expect(result.montant, equals(6000.0));
      expect(result.description, equals('Montant mis à jour'));
      verify(mockRepository.updateCotisation(tContributionId, tUpdatedContribution));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should update contribution status', () async {
      // Arrange
      final statusUpdate = ContributionModel(
        id: tContributionId,
        membreId: 'membre1',
        montant: 5000.0,
        dateEcheance: DateTime(2024, 12, 31),
        annee: 2024,
        type: ContributionType.annuelle,
        statut: ContributionStatus.enRetard,
      );
      when(mockRepository.updateCotisation(tContributionId, statusUpdate))
          .thenAnswer((_) async => statusUpdate);

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

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

    test('should update contribution type', () async {
      // Arrange
      final typeUpdate = ContributionModel(
        id: tContributionId,
        membreId: 'membre1',
        montant: 5000.0,
        dateEcheance: DateTime(2024, 3, 31),
        annee: 2024,
        trimestre: 1,
        type: ContributionType.trimestrielle,
        statut: ContributionStatus.nonPayee,
      );
      when(mockRepository.updateCotisation(tContributionId, typeUpdate))
          .thenAnswer((_) async => typeUpdate);

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

      // Assert
      expect(result.type, equals(ContributionType.trimestrielle));
      expect(result.trimestre, equals(1));
    });

    test('should throw exception when update fails', () async {
      // Arrange
      when(mockRepository.updateCotisation(any, any))
          .thenThrow(Exception('Mise à jour échouée'));

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