/// Tests unitaires pour GetScheduledReports use case
library get_scheduled_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_scheduled_reports.dart';

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

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

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

  group('GetScheduledReports Use Case', () {
    final tScheduledReports = [
      {
        'id': '1',
        'type': 'membres',
        'nom': 'Rapport Membres Mensuel',
        'cronExpression': '0 0 1 * *',
        'active': true,
        'derniereLancement': '2024-01-01T00:00:00Z',
      },
      {
        'id': '2',
        'type': 'cotisations',
        'nom': 'Rapport Cotisations Hebdomadaire',
        'cronExpression': '0 9 * * 1',
        'active': true,
        'derniereLancement': '2024-01-08T09:00:00Z',
      },
    ];

    test('should return list of scheduled reports', () async {
      // Arrange
      when(mockRepository.getScheduledReports())
          .thenAnswer((_) async => tScheduledReports);

      // Act
      final result = await useCase();

      // Assert
      expect(result, equals(tScheduledReports));
      expect(result.length, equals(2));
      expect(result[0]['type'], equals('membres'));
      expect(result[0]['active'], isTrue);
      verify(mockRepository.getScheduledReports());
      verifyNoMoreInteractions(mockRepository);
    });

    test('should return only active scheduled reports', () async {
      // Arrange
      final activeReports = [
        {
          'id': '1',
          'type': 'membres',
          'cronExpression': '0 0 1 * *',
          'active': true,
        },
      ];
      when(mockRepository.getScheduledReports())
          .thenAnswer((_) async => activeReports);

      // Act
      final result = await useCase();

      // Assert
      expect(result.length, equals(1));
      expect(result.first['active'], isTrue);
      verify(mockRepository.getScheduledReports());
    });

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

      // Act
      final result = await useCase();

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

    test('should throw exception when retrieval fails', () async {
      // Arrange
      when(mockRepository.getScheduledReports())
          .thenThrow(Exception('Database error'));

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