/// Tests unitaires pour UpdateSettings use case
library update_settings_test;

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:unionflow_mobile_apps/features/settings/domain/repositories/system_config_repository.dart';
import 'package:unionflow_mobile_apps/features/settings/domain/usecases/update_settings.dart';
import 'package:unionflow_mobile_apps/features/settings/data/models/system_config_model.dart';

@GenerateMocks([ISystemConfigRepository])
import 'update_settings_test.mocks.dart';

void main() {
  late UpdateSettings useCase;
  late MockISystemConfigRepository mockRepository;

  setUp(() {
    mockRepository = MockISystemConfigRepository();
    useCase = UpdateSettings(mockRepository);
  });

  group('UpdateSettings Use Case', () {
    final tConfigMap = {
      'applicationName': 'UnionFlow Updated',
      'maintenanceMode': true,
      'sessionTimeoutMinutes': 45,
    };

    final tUpdatedConfig = SystemConfigModel(
      applicationName: 'UnionFlow Updated',
      maintenanceMode: true,
      sessionTimeoutMinutes: 45,
    );

    test('should update configuration successfully', () async {
      // Arrange
      when(mockRepository.updateConfig(tConfigMap))
          .thenAnswer((_) async => tUpdatedConfig);

      // Act
      final result = await useCase(tConfigMap);

      // Assert
      expect(result, equals(tUpdatedConfig));
      expect(result.applicationName, equals('UnionFlow Updated'));
      expect(result.maintenanceMode, isTrue);
      verify(mockRepository.updateConfig(tConfigMap));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should update partial configuration', () async {
      // Arrange
      final partialConfig = {'maintenanceMode': false};
      final expectedResult = SystemConfigModel(maintenanceMode: false);
      when(mockRepository.updateConfig(partialConfig))
          .thenAnswer((_) async => expectedResult);

      // Act
      final result = await useCase(partialConfig);

      // Assert
      expect(result.maintenanceMode, isFalse);
      verify(mockRepository.updateConfig(partialConfig));
    });

    test('should handle empty config map', () async {
      // Arrange
      final emptyConfig = <String, dynamic>{};
      final expectedResult = SystemConfigModel();
      when(mockRepository.updateConfig(emptyConfig))
          .thenAnswer((_) async => expectedResult);

      // Act
      final result = await useCase(emptyConfig);

      // Assert
      expect(result, isNotNull);
      verify(mockRepository.updateConfig(emptyConfig));
    });

    test('should throw exception when update fails', () async {
      // Arrange
      when(mockRepository.updateConfig(any))
          .thenThrow(Exception('Update failed'));

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