/// Tests unitaires pour GetContributions use case
library get_contributions_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_contributions.dart';
import 'package:unionflow_mobile_apps/features/contributions/data/models/contribution_model.dart';
import 'package:unionflow_mobile_apps/features/contributions/data/repositories/contribution_repository.dart';

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

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

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

  group('GetContributions Use Case', () {
    final tContributionList = [
      ContributionModel(
        id: 'cont1',
        membreId: 'membre1',
        membreNom: 'Dupont',
        membrePrenom: 'Jean',
        montant: 5000.0,
        dateEcheance: DateTime(2024, 12, 31),
        annee: 2024,
        type: ContributionType.mensuelle,
        statut: ContributionStatus.payee,
      ),
      ContributionModel(
        id: 'cont2',
        membreId: 'membre1',
        membreNom: 'Dupont',
        membrePrenom: 'Jean',
        montant: 5000.0,
        dateEcheance: DateTime(2025, 1, 31),
        annee: 2025,
        type: ContributionType.mensuelle,
        statut: ContributionStatus.enAttente,
      ),
    ];

    final tPageResult = ContributionPageResult(
      contributions: tContributionList,
      total: 2,
      totalPages: 1,
      page: 0,
      size: 50,
    );

    test('should return paginated list of contributions', () async {
      // Arrange
      when(mockRepository.getMesCotisations(page: anyNamed('page'), size: anyNamed('size')))
          .thenAnswer((_) async => tPageResult);

      // Act
      final result = await useCase(page: 0, size: 50);

      // Assert
      expect(result, equals(tPageResult));
      expect(result.contributions.length, equals(2));
      expect(result.total, equals(2));
      verify(mockRepository.getMesCotisations(page: 0, size: 50));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should return contributions with custom page size', () async {
      // Arrange
      final smallPageResult = ContributionPageResult(
        contributions: [tContributionList[0]],
        total: 2,
        totalPages: 2,
        page: 0,
        size: 1,
      );
      when(mockRepository.getMesCotisations(page: 0, size: 1))
          .thenAnswer((_) async => smallPageResult);

      // Act
      final result = await useCase(page: 0, size: 1);

      // Assert
      expect(result.contributions.length, equals(1));
      expect(result.size, equals(1));
      verify(mockRepository.getMesCotisations(page: 0, size: 1));
    });

    test('should return empty result when no contributions exist', () async {
      // Arrange
      final emptyResult = ContributionPageResult(
        contributions: [],
        total: 0,
        totalPages: 0,
        page: 0,
        size: 50,
      );
      when(mockRepository.getMesCotisations(page: anyNamed('page'), size: anyNamed('size')))
          .thenAnswer((_) async => emptyResult);

      // Act
      final result = await useCase(page: 0, size: 50);

      // Assert
      expect(result.contributions, isEmpty);
      expect(result.total, equals(0));
    });

    test('should throw exception when repository fails', () async {
      // Arrange
      when(mockRepository.getMesCotisations(page: anyNamed('page'), size: anyNamed('size')))
          .thenThrow(Exception('Network error'));

      // Act & Assert
      expect(() => useCase(page: 0, size: 50), throwsException);
    });
  });
}
