/// Tests unitaires pour GetCacheStats use case
library get_cache_stats_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/get_cache_stats.dart';
import 'package:unionflow_mobile_apps/features/settings/data/models/cache_stats_model.dart';

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

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

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

  group('GetCacheStats Use Case', () {
    final tCacheStats = CacheStatsModel(
      totalEntries: 1000,
      hits: 850,
      misses: 150,
      hitRate: 0.85,
      totalSizeBytes: 1024 * 1024 * 50, // 50 MB
    );

    test('should return cache statistics', () async {
      // Arrange
      when(mockRepository.getCacheStats()).thenAnswer((_) async => tCacheStats);

      // Act
      final result = await useCase();

      // Assert
      expect(result, equals(tCacheStats));
      expect(result.totalEntries, equals(1000));
      expect(result.hitRate, equals(0.85));
      verify(mockRepository.getCacheStats());
      verifyNoMoreInteractions(mockRepository);
    });

    test('should handle empty cache', () async {
      // Arrange
      final emptyCacheStats = CacheStatsModel(
        totalEntries: 0,
        hits: 0,
        misses: 0,
        hitRate: 0.0,
        totalSizeBytes: 0,
      );
      when(mockRepository.getCacheStats())
          .thenAnswer((_) async => emptyCacheStats);

      // Act
      final result = await useCase();

      // Assert
      expect(result.totalEntries, equals(0));
      expect(result.hitRate, equals(0.0));
    });

    test('should handle low hit rate cache', () async {
      // Arrange
      final lowHitCacheStats = CacheStatsModel(
        totalEntries: 100,
        hits: 20,
        misses: 80,
        hitRate: 0.20,
        totalSizeBytes: 1024 * 100,
      );
      when(mockRepository.getCacheStats())
          .thenAnswer((_) async => lowHitCacheStats);

      // Act
      final result = await useCase();

      // Assert
      expect(result.hitRate, lessThan(0.5));
      expect(result.misses!, greaterThan(result.hits!));
    });

    test('should throw exception when stats retrieval fails', () async {
      // Arrange
      when(mockRepository.getCacheStats())
          .thenThrow(Exception('Stats unavailable'));

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