/// Tests unitaires pour GetPendingApprovals use case
library get_pending_approvals_test;

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:dartz/dartz.dart';
import 'package:unionflow_mobile_apps/features/finance_workflow/domain/usecases/get_pending_approvals.dart';
import 'package:unionflow_mobile_apps/features/finance_workflow/domain/repositories/finance_workflow_repository.dart';
import 'package:unionflow_mobile_apps/features/finance_workflow/domain/entities/transaction_approval.dart';
import 'package:unionflow_mobile_apps/core/error/failures.dart';

@GenerateMocks([FinanceWorkflowRepository])
import 'get_pending_approvals_test.mocks.dart';

void main() {
  late GetPendingApprovals useCase;
  late MockFinanceWorkflowRepository mockRepository;

  setUp(() {
    mockRepository = MockFinanceWorkflowRepository();
    useCase = GetPendingApprovals(mockRepository);
  });

  group('GetPendingApprovals Use Case', () {
    final tApprovals = [
      TransactionApproval(
        id: 'approval-1',
        transactionId: 'tx-123',
        transactionType: TransactionType.withdrawal,
        amount: 500000.0,
        currency: 'XOF',
        requesterId: 'user-1',
        requesterName: 'Amadou Diallo',
        organizationId: 'org-123',
        requiredLevel: ApprovalLevel.level2,
        status: ApprovalStatus.pending,
        approvers: [],
        createdAt: DateTime(2024, 12, 15),
      ),
      TransactionApproval(
        id: 'approval-2',
        transactionId: 'tx-456',
        transactionType: TransactionType.solidarity,
        amount: 200000.0,
        currency: 'XOF',
        requesterId: 'user-2',
        requesterName: 'Fatou Ndiaye',
        requiredLevel: ApprovalLevel.level1,
        status: ApprovalStatus.pending,
        approvers: [],
        createdAt: DateTime(2024, 12, 14),
      ),
    ];

    test('should return list of pending approvals successfully', () async {
      // Arrange
      when(mockRepository.getPendingApprovals(
        organizationId: anyNamed('organizationId'),
      )).thenAnswer((_) async => Right(tApprovals));

      // Act
      final result = await useCase(organizationId: 'org-123');

      // Assert
      expect(result, Right(tApprovals));
      result.fold(
        (failure) => fail('Should not return failure'),
        (approvals) {
          expect(approvals.length, equals(2));
          expect(approvals[0].status, equals(ApprovalStatus.pending));
          expect(approvals[0].amount, equals(500000.0));
        },
      );
      verify(mockRepository.getPendingApprovals(organizationId: 'org-123'));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should return approvals with different levels', () async {
      // Arrange
      when(mockRepository.getPendingApprovals(
        organizationId: anyNamed('organizationId'),
      )).thenAnswer((_) async => Right(tApprovals));

      // Act
      final result = await useCase();

      // Assert
      result.fold(
        (failure) => fail('Should not return failure'),
        (approvals) {
          expect(approvals.any((a) => a.requiredLevel == ApprovalLevel.level2), isTrue);
          expect(approvals.any((a) => a.requiredLevel == ApprovalLevel.level1), isTrue);
        },
      );
    });

    test('should return empty list when no pending approvals', () async {
      // Arrange
      when(mockRepository.getPendingApprovals(
        organizationId: anyNamed('organizationId'),
      )).thenAnswer((_) async => Right([]));

      // Act
      final result = await useCase();

      // Assert
      result.fold(
        (failure) => fail('Should not return failure'),
        (approvals) => expect(approvals, isEmpty),
      );
    });

    test('should return ServerFailure when repository fails', () async {
      // Arrange
      final tFailure = ServerFailure('Erreur serveur');
      when(mockRepository.getPendingApprovals(
        organizationId: anyNamed('organizationId'),
      )).thenAnswer((_) async => Left(tFailure));

      // Act
      final result = await useCase();

      // Assert
      expect(result, Left(tFailure));
      result.fold(
        (failure) => expect(failure, isA<ServerFailure>()),
        (approvals) => fail('Should not return approvals'),
      );
    });
  });
}
