/// Tests unitaires pour ExportReportPdf use case
library export_report_pdf_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/export_report_pdf.dart';

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

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

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

  group('ExportReportPdf Use Case', () {
    const tReportType = 'membres';
    const tPdfPath = '/storage/reports/membres_2024.pdf';

    test('should export report to PDF successfully', () async {
      // Arrange
      when(mockRepository.exportReportPdf(tReportType))
          .thenAnswer((_) async => tPdfPath);

      // Act
      final result = await useCase(tReportType);

      // Assert
      expect(result, equals(tPdfPath));
      expect(result, contains('.pdf'));
      verify(mockRepository.exportReportPdf(tReportType));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should return valid PDF path with type name', () async {
      // Arrange
      const cotisationsType = 'cotisations';
      const cotisationsPdf = '/storage/reports/cotisations_2024.pdf';
      when(mockRepository.exportReportPdf(cotisationsType))
          .thenAnswer((_) async => cotisationsPdf);

      // Act
      final result = await useCase(cotisationsType);

      // Assert
      expect(result, contains('cotisations'));
      expect(result, endsWith('.pdf'));
    });

    test('should return URL for remote PDF file', () async {
      // Arrange
      const remotePdf = 'https://api.example.com/reports/membres.pdf';
      when(mockRepository.exportReportPdf(tReportType))
          .thenAnswer((_) async => remotePdf);

      // Act
      final result = await useCase(tReportType);

      // Assert
      expect(result, startsWith('https://'));
      expect(result, endsWith('.pdf'));
    });

    test('should throw exception when PDF export fails', () async {
      // Arrange
      when(mockRepository.exportReportPdf(any))
          .thenThrow(Exception('PDF generation failed'));

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