import 'package:dartz/dartz.dart';
import 'package:injectable/injectable.dart';
import '../../repositories/license_repository.dart';
import '../../../core/errors/failures.dart';
import '../base_usecase.dart';

/// 라이선스 삭제 UseCase
@injectable
class DeleteLicenseUseCase implements UseCase<bool, int> {
  final LicenseRepository repository;

  DeleteLicenseUseCase(this.repository);

  @override
  Future<Either<Failure, bool>> call(int id) async {
    try {
      // 비즈니스 로직: 활성 라이선스는 삭제 불가
      final licenseResult = await repository.getLicenseById(id);
      return licenseResult.fold(
        (failure) => Left(failure),
        (license) async {
          if (license.isActive == true) {
            return Left(ValidationFailure(message: '활성 라이선스는 삭제할 수 없습니다'));
          }

          final deleteResult = await repository.deleteLicense(id);
          return deleteResult.fold(
            (failure) => Left(failure),
            (_) => const Right(true),
          );
        },
      );
    } catch (e) {
      return Left(ServerFailure(message: e.toString()));
    }
  }
}