/// Tests unitaires pour ResetSettings use case
library reset_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/reset_settings.dart';
import 'package:unionflow_mobile_apps/features/settings/data/models/system_config_model.dart';

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

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

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

  group('ResetSettings Use Case', () {
    final tDefaultConfig = SystemConfigModel(
      applicationName: 'UnionFlow',
      version: '1.0.0',
      maintenanceMode: false,
      defaultLanguage: 'fr',
      timezone: 'Europe/Paris',
    );

    test('should reset configuration to default values', () async {
      // Arrange
      when(mockRepository.resetConfig()).thenAnswer((_) async => tDefaultConfig);

      // Act
      final result = await useCase();

      // Assert
      expect(result, equals(tDefaultConfig));
      expect(result.maintenanceMode, isFalse);
      expect(result.applicationName, equals('UnionFlow'));
      expect(result.version, equals('1.0.0'));
      verify(mockRepository.resetConfig());
      verifyNoMoreInteractions(mockRepository);
    });

    test('should handle fallback when reset endpoint fails', () async {
      // Arrange
      when(mockRepository.resetConfig()).thenAnswer((_) async => tDefaultConfig);

      // Act
      final result = await useCase();

      // Assert
      expect(result, isNotNull);
      expect(result.applicationName, equals('UnionFlow'));
      verify(mockRepository.resetConfig());
    });

    test('should throw exception when all reset strategies fail', () async {
      // Arrange
      when(mockRepository.resetConfig()).thenThrow(Exception('Reset failed'));

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

    test('should return valid config with minimal required fields', () async {
      // Arrange
      final tMinimalConfig = SystemConfigModel(
        applicationName: 'UnionFlow',
        version: '1.0.0',
        maintenanceMode: false,
      );
      when(mockRepository.resetConfig()).thenAnswer((_) async => tMinimalConfig);

      // Act
      final result = await useCase();

      // Assert
      expect(result.applicationName, isNotNull);
      expect(result.version, isNotNull);
      expect(result.maintenanceMode, isNotNull);
      verify(mockRepository.resetConfig());
    });
  });
}
