/// Tests unitaires pour GetCompteAdherent use case
library get_compte_adherent_test;

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:dartz/dartz.dart';
import 'package:unionflow_mobile_apps/features/dashboard/domain/usecases/get_compte_adherent.dart';
import 'package:unionflow_mobile_apps/features/dashboard/domain/repositories/dashboard_repository.dart';
import 'package:unionflow_mobile_apps/features/dashboard/domain/entities/compte_adherent_entity.dart';
import 'package:unionflow_mobile_apps/core/error/failures.dart';
import 'package:unionflow_mobile_apps/core/usecases/usecase.dart';

@GenerateMocks([DashboardRepository])
import 'get_compte_adherent_test.mocks.dart';

void main() {
  late GetCompteAdherent useCase;
  late MockDashboardRepository mockRepository;

  setUp(() {
    mockRepository = MockDashboardRepository();
    useCase = GetCompteAdherent(mockRepository);
  });

  group('GetCompteAdherent Use Case', () {
    final tCompteAdherent = CompteAdherentEntity(
      numeroMembre: 'M-2024-001',
      nomComplet: 'Amadou Diallo',
      organisationNom: 'Association Alpha',
      dateAdhesion: DateTime(2024, 1, 15),
      statutCompte: 'ACTIF',
      soldeCotisations: 50000.0,
      soldeEpargne: 125000.0,
      soldeBloque: 15000.0,
      soldeTotalDisponible: 160000.0,
      encoursCreditTotal: 75000.0,
      capaciteEmprunt: 200000.0,
      nombreCotisationsPayees: 12,
      nombreCotisationsTotal: 12,
      nombreCotisationsEnRetard: 0,
      engagementRate: 1.0,
      nombreComptesEpargne: 2,
      dateCalcul: DateTime(2024, 12, 15),
    );

    test('should return compte adherent successfully', () async {
      // Arrange
      when(mockRepository.getCompteAdherent())
          .thenAnswer((_) async => Right(tCompteAdherent));

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

      // Assert
      expect(result, Right(tCompteAdherent));
      result.fold(
        (failure) => fail('Should not return failure'),
        (compte) {
          expect(compte.numeroMembre, equals('M-2024-001'));
          expect(compte.nomComplet, equals('Amadou Diallo'));
          expect(compte.soldeTotalDisponible, equals(160000.0));
          expect(compte.capaciteEmprunt, equals(200000.0));
        },
      );
      verify(mockRepository.getCompteAdherent());
      verifyNoMoreInteractions(mockRepository);
    });

    test('should return compte with multiple epargne accounts', () async {
      // Arrange
      when(mockRepository.getCompteAdherent())
          .thenAnswer((_) async => Right(tCompteAdherent));

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

      // Assert
      result.fold(
        (failure) => fail('Should not return failure'),
        (compte) {
          expect(compte.nombreComptesEpargne, equals(2));
          expect(compte.soldeEpargne, equals(125000.0));
          expect(compte.engagementRate, equals(1.0));
        },
      );
    });

    test('should return compte with overdue contributions', () async {
      // Arrange
      final compteWithOverdue = CompteAdherentEntity(
        numeroMembre: 'M-2024-002',
        nomComplet: 'Fatou Ndiaye',
        statutCompte: 'ACTIF',
        soldeCotisations: 25000.0,
        soldeEpargne: 50000.0,
        soldeBloque: 0.0,
        soldeTotalDisponible: 75000.0,
        encoursCreditTotal: 0.0,
        capaciteEmprunt: 100000.0,
        nombreCotisationsPayees: 8,
        nombreCotisationsTotal: 12,
        nombreCotisationsEnRetard: 4,
        engagementRate: 0.67,
        nombreComptesEpargne: 1,
        dateCalcul: DateTime(2024, 12, 15),
      );
      when(mockRepository.getCompteAdherent())
          .thenAnswer((_) async => Right(compteWithOverdue));

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

      // Assert
      result.fold(
        (failure) => fail('Should not return failure'),
        (compte) {
          expect(compte.nombreCotisationsEnRetard, equals(4));
          expect(compte.engagementRate, lessThan(1.0));
        },
      );
    });

    test('should return ServerFailure when repository fails', () async {
      // Arrange
      final tFailure = ServerFailure('Erreur serveur');
      when(mockRepository.getCompteAdherent())
          .thenAnswer((_) async => Left(tFailure));

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

      // Assert
      expect(result, Left(tFailure));
      result.fold(
        (failure) => expect(failure, isA<ServerFailure>()),
        (compte) => fail('Should not return compte'),
      );
    });
  });
}
