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

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

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

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

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

    final tUpdatedMembre = MembreCompletModel(
      id: tMembreId,
      nom: 'Dupont',
      prenom: 'Jean',
      email: 'jean.dupont@example.com',
      telephone: '+33698765432', // Updated phone
      dateNaissance: DateTime(1990, 1, 1),
      adresse: '123 Rue de Paris', // Added address
    );

    test('should update profile successfully', () async {
      // Arrange
      when(mockRepository.updateProfile(tMembreId, tMembre))
          .thenAnswer((_) async => tUpdatedMembre);

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

      // Assert
      expect(result, equals(tUpdatedMembre));
      verify(mockRepository.updateProfile(tMembreId, tMembre));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should update only specified fields', () async {
      // Arrange
      when(mockRepository.updateProfile(any, any))
          .thenAnswer((_) async => tUpdatedMembre);

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

      // Assert
      expect(result.telephone, equals('+33698765432'));
      expect(result.adresse, equals('123 Rue de Paris'));
      verify(mockRepository.updateProfile(tMembreId, tMembre));
    });

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

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

    test('should throw exception when update fails', () async {
      // Arrange
      when(mockRepository.updateProfile(any, any))
          .thenThrow(Exception('Network error'));

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