/// Tests unitaires pour UpdatePreferences use case
library update_preferences_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_preferences.dart';

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

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

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

  group('UpdatePreferences Use Case', () {
    const tMembreId = 'membre1';
    final tPreferences = {
      'language': 'fr',
      'theme': 'dark',
      'notifications': true,
      'emailNotifications': false,
    };

    final tUpdatedPreferences = {
      ...tPreferences,
      'lastUpdated': '2026-03-14T10:00:00Z',
    };

    test('should update preferences successfully', () async {
      // Arrange
      when(mockRepository.updatePreferences(tMembreId, tPreferences))
          .thenAnswer((_) async => tUpdatedPreferences);

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

      // Assert
      expect(result, equals(tUpdatedPreferences));
      expect(result['language'], equals('fr'));
      expect(result['theme'], equals('dark'));
      verify(mockRepository.updatePreferences(tMembreId, tPreferences));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should update partial preferences', () async {
      // Arrange
      final partialPrefs = {'theme': 'light'};
      final expectedResult = {'theme': 'light'};
      when(mockRepository.updatePreferences(tMembreId, partialPrefs))
          .thenAnswer((_) async => expectedResult);

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

      // Assert
      expect(result['theme'], equals('light'));
      verify(mockRepository.updatePreferences(tMembreId, partialPrefs));
    });

    test('should handle empty preferences map', () async {
      // Arrange
      final emptyPrefs = <String, dynamic>{};
      when(mockRepository.updatePreferences(tMembreId, emptyPrefs))
          .thenAnswer((_) async => emptyPrefs);

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

      // Assert
      expect(result, isEmpty);
      verify(mockRepository.updatePreferences(tMembreId, emptyPrefs));
    });

    test('should throw exception when update fails', () async {
      // Arrange
      when(mockRepository.updatePreferences(any, any))
          .thenThrow(Exception('Failed to update preferences'));

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