/// Tests unitaires pour ExportReportExcel use case
library export_report_excel_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_excel.dart';

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

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

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

  group('ExportReportExcel Use Case', () {
    const tReportType = 'membres';
    const tExcelPath = '/storage/reports/membres_2024.xlsx';
    const tCsvPath = '/storage/reports/membres_2024.csv';

    test('should export report to Excel successfully', () async {
      // Arrange
      when(mockRepository.exportReportExcel(tReportType, format: 'excel'))
          .thenAnswer((_) async => tExcelPath);

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

      // Assert
      expect(result, equals(tExcelPath));
      expect(result, contains('.xlsx'));
      verify(mockRepository.exportReportExcel(tReportType, format: 'excel'));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should export report to CSV when format specified', () async {
      // Arrange
      when(mockRepository.exportReportExcel(tReportType, format: 'csv'))
          .thenAnswer((_) async => tCsvPath);

      // Act
      final result = await useCase(tReportType, format: 'csv');

      // Assert
      expect(result, equals(tCsvPath));
      expect(result, contains('.csv'));
      verify(mockRepository.exportReportExcel(tReportType, format: 'csv'));
    });

    test('should return valid Excel path with type name', () async {
      // Arrange
      const cotisationsType = 'cotisations';
      const cotisationsExcel = '/storage/reports/cotisations_2024.xlsx';
      when(mockRepository.exportReportExcel(cotisationsType, format: 'excel'))
          .thenAnswer((_) async => cotisationsExcel);

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

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

    test('should throw exception when Excel export fails', () async {
      // Arrange
      when(mockRepository.exportReportExcel(any, format: anyNamed('format')))
          .thenThrow(Exception('Excel export failed'));

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