/// Tests unitaires pour GetProfile use case
library get_profile_test;

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:unionflow_mobile_apps/features/profile/domain/repositories/profile_repository.dart';
import 'package:unionflow_mobile_apps/features/profile/domain/usecases/get_profile.dart';
import 'package:unionflow_mobile_apps/features/members/data/models/membre_complete_model.dart';

@GenerateMocks([IProfileRepository])
import 'get_profile_test.mocks.dart';

void main() {
  late GetProfile useCase;
  late MockIProfileRepository mockRepository;

  setUp(() {
    mockRepository = MockIProfileRepository();
    useCase = GetProfile(mockRepository);
  });

  group('GetProfile Use Case', () {
    final tMembre = MembreCompletModel(
      id: 'membre1',
      nom: 'Dupont',
      prenom: 'Jean',
      email: 'jean.dupont@example.com',
      telephone: '+33612345678',
      dateNaissance: DateTime(1990, 1, 1),
    );

    test('should return current user profile from repository', () async {
      // Arrange
      when(mockRepository.getMe()).thenAnswer((_) async => tMembre);

      // Act
      final result = await useCase();

      // Assert
      expect(result, equals(tMembre));
      verify(mockRepository.getMe());
      verifyNoMoreInteractions(mockRepository);
    });

    test('should return null when user is not authenticated', () async {
      // Arrange
      when(mockRepository.getMe()).thenAnswer((_) async => null);

      // Act
      final result = await useCase();

      // Assert
      expect(result, isNull);
      verify(mockRepository.getMe());
    });

    test('should throw exception when repository throws', () async {
      // Arrange
      when(mockRepository.getMe()).thenThrow(Exception('Unauthorized'));

      // Act & Assert
      expect(
        () => useCase(),
        throwsA(isA<Exception>()),
      );
      verify(mockRepository.getMe());
    });

    test('should cache profile data on successful retrieval', () async {
      // Arrange
      when(mockRepository.getMe()).thenAnswer((_) async => tMembre);

      // Act - Call twice
      final result1 = await useCase();
      final result2 = await useCase();

      // Assert - Repository called twice (no caching at use case level)
      expect(result1, equals(tMembre));
      expect(result2, equals(tMembre));
      verify(mockRepository.getMe()).called(2);
    });
  });
}
