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

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

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

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

  group('CreateContribution Use Case', () {
    final tNewContribution = ContributionModel(
      membreId: 'membre1',
      montant: 5000.0,
      dateEcheance: DateTime(2024, 12, 31),
      annee: 2024,
      type: ContributionType.annuelle,
      statut: ContributionStatus.nonPayee,
    );

    final tCreatedContribution = ContributionModel(
      id: 'cont123',
      membreId: 'membre1',
      membreNom: 'Dupont',
      membrePrenom: 'Jean',
      montant: 5000.0,
      dateEcheance: DateTime(2024, 12, 31),
      annee: 2024,
      type: ContributionType.annuelle,
      statut: ContributionStatus.nonPayee,
    );

    test('should create contribution successfully', () async {
      // Arrange
      when(mockRepository.createCotisation(tNewContribution))
          .thenAnswer((_) async => tCreatedContribution);

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

      // Assert
      expect(result, equals(tCreatedContribution));
      expect(result.id, isNotNull);
      expect(result.id, equals('cont123'));
      expect(result.montant, equals(5000.0));
      verify(mockRepository.createCotisation(tNewContribution));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should create monthly contribution', () async {
      // Arrange
      final monthlyContribution = ContributionModel(
        membreId: 'membre1',
        montant: 2000.0,
        dateEcheance: DateTime(2024, 1, 31),
        annee: 2024,
        mois: 1,
        type: ContributionType.mensuelle,
        statut: ContributionStatus.nonPayee,
      );
      final createdMonthly = ContributionModel(
        id: 'cont456',
        membreId: 'membre1',
        montant: 2000.0,
        dateEcheance: DateTime(2024, 1, 31),
        annee: 2024,
        mois: 1,
        type: ContributionType.mensuelle,
        statut: ContributionStatus.nonPayee,
      );
      when(mockRepository.createCotisation(monthlyContribution))
          .thenAnswer((_) async => createdMonthly);

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

      // Assert
      expect(result.type, equals(ContributionType.mensuelle));
      expect(result.mois, equals(1));
    });

    test('should create contribution with description', () async {
      // Arrange
      final contributionWithDesc = ContributionModel(
        membreId: 'membre1',
        montant: 5000.0,
        dateEcheance: DateTime(2024, 12, 31),
        annee: 2024,
        type: ContributionType.exceptionnelle,
        statut: ContributionStatus.nonPayee,
        description: 'Cotisation exceptionnelle pour projet spécial',
      );
      when(mockRepository.createCotisation(any))
          .thenAnswer((_) async => contributionWithDesc.copyWith(id: 'cont789'));

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

      // Assert
      expect(result.description, isNotNull);
      expect(result.type, equals(ContributionType.exceptionnelle));
    });

    test('should throw exception when creation fails', () async {
      // Arrange
      when(mockRepository.createCotisation(any))
          .thenThrow(Exception('Erreur de validation'));

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