/// Tests unitaires pour GetBudgets use case
library get_budgets_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_budgets.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/budget.dart';
import 'package:unionflow_mobile_apps/core/error/failures.dart';

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

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

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

  group('GetBudgets Use Case', () {
    final tBudgets = [
      Budget(
        id: 'budget-1',
        name: 'Budget Annuel 2024',
        organizationId: 'org-123',
        period: BudgetPeriod.annual,
        year: 2024,
        status: BudgetStatus.active,
        lines: [],
        totalPlanned: 5000000.0,
        totalRealized: 3250000.0,
        currency: 'XOF',
        createdBy: 'user-1',
        createdAt: DateTime(2024, 1, 1),
        startDate: DateTime(2024, 1, 1),
        endDate: DateTime(2024, 12, 31),
      ),
      Budget(
        id: 'budget-2',
        name: 'Budget Q4 2024',
        organizationId: 'org-123',
        period: BudgetPeriod.quarterly,
        year: 2024,
        month: 10,
        status: BudgetStatus.active,
        lines: [],
        totalPlanned: 1250000.0,
        totalRealized: 850000.0,
        currency: 'XOF',
        createdBy: 'user-1',
        createdAt: DateTime(2024, 10, 1),
        startDate: DateTime(2024, 10, 1),
        endDate: DateTime(2024, 12, 31),
      ),
    ];

    test('should return list of budgets successfully', () async {
      // Arrange
      when(mockRepository.getBudgets(
        organizationId: anyNamed('organizationId'),
        status: anyNamed('status'),
        year: anyNamed('year'),
      )).thenAnswer((_) async => Right(tBudgets));

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

      // Assert
      expect(result, Right(tBudgets));
      result.fold(
        (failure) => fail('Should not return failure'),
        (budgets) {
          expect(budgets.length, equals(2));
          expect(budgets[0].name, equals('Budget Annuel 2024'));
          expect(budgets[0].totalPlanned, equals(5000000.0));
        },
      );
      verify(mockRepository.getBudgets(
        organizationId: 'org-123',
        status: null,
        year: null,
      ));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should filter budgets by status and year', () async {
      // Arrange
      final activeBudgets = [tBudgets[0], tBudgets[1]];
      when(mockRepository.getBudgets(
        organizationId: 'org-123',
        status: BudgetStatus.active,
        year: 2024,
      )).thenAnswer((_) async => Right(activeBudgets));

      // Act
      final result = await useCase(
        organizationId: 'org-123',
        status: BudgetStatus.active,
        year: 2024,
      );

      // Assert
      result.fold(
        (failure) => fail('Should not return failure'),
        (budgets) {
          expect(budgets.every((b) => b.status == BudgetStatus.active), isTrue);
          expect(budgets.every((b) => b.year == 2024), isTrue);
        },
      );
    });

    test('should return empty list when no budgets exist', () async {
      // Arrange
      when(mockRepository.getBudgets(
        organizationId: anyNamed('organizationId'),
        status: anyNamed('status'),
        year: anyNamed('year'),
      )).thenAnswer((_) async => Right([]));

      // Act
      final result = await useCase();

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

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

      // Act
      final result = await useCase();

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