/// Tests unitaires pour ChangePassword use case
library change_password_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/change_password.dart';

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

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

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

  group('ChangePassword Use Case', () {
    const tMembreId = 'membre1';
    const tOldPassword = 'OldPassword123!';
    const tNewPassword = 'NewPassword456!';

    test('should change password successfully', () async {
      // Arrange
      when(mockRepository.changePassword(tMembreId, tOldPassword, tNewPassword))
          .thenAnswer((_) async => Future.value());

      // Act
      await useCase(tMembreId, tOldPassword, tNewPassword);

      // Assert
      verify(mockRepository.changePassword(tMembreId, tOldPassword, tNewPassword));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should throw exception when old password is incorrect', () async {
      // Arrange
      when(mockRepository.changePassword(any, any, any))
          .thenThrow(Exception('Mot de passe incorrect'));

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

    test('should throw exception when new password is too weak', () async {
      // Arrange
      when(mockRepository.changePassword(any, any, any))
          .thenThrow(Exception('Mot de passe trop faible'));

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

    test('should throw exception when Keycloak service fails', () async {
      // Arrange
      when(mockRepository.changePassword(any, any, any))
          .thenThrow(Exception('Keycloak service unavailable'));

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