/// Tests unitaires pour ScheduleReport use case
library schedule_report_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/schedule_report.dart';

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

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

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

  group('ScheduleReport Use Case', () {
    const tCronExpression = '0 0 1 * *'; // 1er de chaque mois à minuit

    test('should schedule report successfully', () async {
      // Arrange
      when(mockRepository.scheduleReport(cronExpression: null))
          .thenAnswer((_) async => Future.value());

      // Act
      await useCase();

      // Assert
      verify(mockRepository.scheduleReport(cronExpression: null));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should schedule report with cron expression', () async {
      // Arrange
      when(mockRepository.scheduleReport(cronExpression: tCronExpression))
          .thenAnswer((_) async => Future.value());

      // Act
      await useCase(cronExpression: tCronExpression);

      // Assert
      verify(mockRepository.scheduleReport(cronExpression: tCronExpression));
    });

    test('should schedule report with different cron expressions', () async {
      // Arrange
      const weeklyCron = '0 9 * * 1'; // Tous les lundis à 9h
      when(mockRepository.scheduleReport(cronExpression: weeklyCron))
          .thenAnswer((_) async => Future.value());

      // Act
      await useCase(cronExpression: weeklyCron);

      // Assert
      verify(mockRepository.scheduleReport(cronExpression: weeklyCron));
    });

    test('should throw exception when scheduling fails', () async {
      // Arrange
      when(mockRepository.scheduleReport(cronExpression: anyNamed('cronExpression')))
          .thenThrow(Exception('Invalid cron expression'));

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