/// Tests unitaires pour GetOrganizationMembers use case
library get_organization_members_test;

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:unionflow_mobile_apps/features/organizations/domain/repositories/organization_repository.dart';
import 'package:unionflow_mobile_apps/features/organizations/domain/usecases/get_organization_members.dart';

@GenerateMocks([IOrganizationRepository])
import 'get_organization_members_test.mocks.dart';

void main() {
  late GetOrganizationMembers useCase;
  late MockIOrganizationRepository mockRepository;

  setUp(() {
    mockRepository = MockIOrganizationRepository();
    useCase = GetOrganizationMembers(mockRepository);
  });

  group('GetOrganizationMembers Use Case', () {
    const tOrganizationId = 'org1';
    final tMembersList = [
      {
        'id': 'membre1',
        'nom': 'Dupont',
        'prenom': 'Jean',
        'email': 'jean.dupont@example.com',
      },
      {
        'id': 'membre2',
        'nom': 'Martin',
        'prenom': 'Marie',
        'email': 'marie.martin@example.com',
      },
    ];

    test('should return list of organization members', () async {
      // Arrange
      when(mockRepository.getOrganizationMembers(tOrganizationId))
          .thenAnswer((_) async => tMembersList);

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

      // Assert
      expect(result, equals(tMembersList));
      expect(result.length, equals(2));
      verify(mockRepository.getOrganizationMembers(tOrganizationId));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should return empty list when organization has no members', () async {
      // Arrange
      when(mockRepository.getOrganizationMembers(tOrganizationId))
          .thenAnswer((_) async => []);

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

      // Assert
      expect(result, isEmpty);
      verify(mockRepository.getOrganizationMembers(tOrganizationId));
    });

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

      // Act & Assert
      expect(
        () => useCase(tOrganizationId),
        throwsA(isA<Exception>()),
      );
    });

    test('should throw exception when retrieval fails', () async {
      // Arrange
      when(mockRepository.getOrganizationMembers(any))
          .thenThrow(Exception('Erreur de récupération'));

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