/// Use case: Créer un budget
library create_budget;

import 'package:dartz/dartz.dart';
import 'package:injectable/injectable.dart';
import '../../../../core/error/failures.dart';
import '../entities/budget.dart';
import '../repositories/finance_workflow_repository.dart';

@lazySingleton
class CreateBudget {
  final FinanceWorkflowRepository repository;

  CreateBudget(this.repository);

  Future<Either<Failure, Budget>> call({
    required String name,
    String? description,
    required String organizationId,
    required BudgetPeriod period,
    required int year,
    int? month,
    required List<BudgetLine> lines,
  }) async {
    // Validation
    if (name.trim().isEmpty) {
      return Left(ValidationFailure('Nom du budget requis'));
    }

    if (organizationId.isEmpty) {
      return Left(ValidationFailure('ID organisation requis'));
    }

    if (year < 2000 || year > 2100) {
      return Left(ValidationFailure('Année invalide'));
    }

    if (period == BudgetPeriod.monthly && (month == null || month < 1 || month > 12)) {
      return Left(ValidationFailure('Mois requis pour budget mensuel (1-12)'));
    }

    if (lines.isEmpty) {
      return Left(ValidationFailure('Au moins une ligne budgétaire requise'));
    }

    return await repository.createBudget(
      name: name,
      description: description,
      organizationId: organizationId,
      period: period,
      year: year,
      month: month,
      lines: lines,
    );
  }
}
