/// Tests unitaires pour GetReports use case
library get_reports_test;

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:unionflow_mobile_apps/features/reports/domain/repositories/reports_repository.dart';
import 'package:unionflow_mobile_apps/features/reports/domain/usecases/get_reports.dart';

@GenerateMocks([IReportsRepository])
import 'get_reports_test.mocks.dart';

void main() {
  late GetReports useCase;
  late MockIReportsRepository mockRepository;

  setUp(() {
    mockRepository = MockIReportsRepository();
    useCase = GetReports(mockRepository);
  });

  group('GetReports Use Case', () {
    final tAvailableReports = [
      {
        'type': 'membres',
        'nom': 'Rapport Membres',
        'description': 'Statistiques complètes des membres',
        'categorie': 'administratif',
      },
      {
        'type': 'cotisations',
        'nom': 'Rapport Cotisations',
        'description': 'Analyse des cotisations',
        'categorie': 'financier',
      },
      {
        'type': 'evenements',
        'nom': 'Rapport Événements',
        'description': 'Bilan des événements',
        'categorie': 'activites',
      },
    ];

    test('should return list of available reports', () async {
      // Arrange
      when(mockRepository.getAvailableReports())
          .thenAnswer((_) async => tAvailableReports);

      // Act
      final result = await useCase();

      // Assert
      expect(result, equals(tAvailableReports));
      expect(result.length, equals(3));
      expect(result[0]['type'], equals('membres'));
      verify(mockRepository.getAvailableReports());
      verifyNoMoreInteractions(mockRepository);
    });

    test('should return reports with specific categories', () async {
      // Arrange
      final financialReports = [
        {
          'type': 'cotisations',
          'nom': 'Rapport Cotisations',
          'categorie': 'financier',
        },
      ];
      when(mockRepository.getAvailableReports())
          .thenAnswer((_) async => financialReports);

      // Act
      final result = await useCase();

      // Assert
      expect(result.length, equals(1));
      expect(result.first['categorie'], equals('financier'));
      verify(mockRepository.getAvailableReports());
    });

    test('should return empty list when no reports available', () async {
      // Arrange
      when(mockRepository.getAvailableReports()).thenAnswer((_) async => []);

      // Act
      final result = await useCase();

      // Assert
      expect(result, isEmpty);
      verify(mockRepository.getAvailableReports());
    });

    test('should throw exception when repository fails', () async {
      // Arrange
      when(mockRepository.getAvailableReports())
          .thenThrow(Exception('Network error'));

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