/// Tests unitaires pour UpdateAvatar use case
library update_avatar_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/update_avatar.dart';
import 'package:unionflow_mobile_apps/features/members/data/models/membre_complete_model.dart';

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

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

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

  group('UpdateAvatar Use Case', () {
    const tMembreId = 'membre1';
    const tPhotoUrl = 'https://example.com/avatar.jpg';

    final tUpdatedMembre = MembreCompletModel(
      id: tMembreId,
      nom: 'Dupont',
      prenom: 'Jean',
      email: 'jean.dupont@example.com',
      photo: tPhotoUrl,
    );

    test('should update avatar successfully', () async {
      // Arrange
      when(mockRepository.updateAvatar(tMembreId, tPhotoUrl))
          .thenAnswer((_) async => tUpdatedMembre);

      // Act
      final result = await useCase(tMembreId, tPhotoUrl);

      // Assert
      expect(result, equals(tUpdatedMembre));
      expect(result.photo, equals(tPhotoUrl));
      verify(mockRepository.updateAvatar(tMembreId, tPhotoUrl));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should handle empty photo URL', () async {
      // Arrange
      const emptyUrl = '';
      final emptyPhotoMembre = MembreCompletModel(
        id: tMembreId,
        nom: 'Dupont',
        prenom: 'Jean',
        email: 'jean.dupont@example.com',
        photo: emptyUrl,
      );
      when(mockRepository.updateAvatar(tMembreId, emptyUrl))
          .thenAnswer((_) async => emptyPhotoMembre);

      // Act
      final result = await useCase(tMembreId, emptyUrl);

      // Assert
      expect(result.photo, equals(emptyUrl));
      verify(mockRepository.updateAvatar(tMembreId, emptyUrl));
    });

    test('should throw exception when member not found', () async {
      // Arrange
      when(mockRepository.updateAvatar(any, any))
          .thenThrow(Exception('Membre non trouvé'));

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

    test('should throw exception when upload fails', () async {
      // Arrange
      when(mockRepository.updateAvatar(any, any))
          .thenThrow(Exception('Upload failed'));

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