import 'package:boilerplate/core/utils/shared_preferences_util.dart';
import 'package:boilerplate/data/repositories/product_repository.dart';
import 'package:dartz/dartz.dart';
import 'package:injectable/injectable.dart';

import '../../../core/utils/print_utils.dart';
import '../../../data/models/app/failure.dart';

@injectable
class CheckProductCartUseCase {
  final ProductRepository _repo;
  @factoryParam
  final SharedPreferencesUtil _pref;

  CheckProductCartUseCase(this._repo, this._pref);

  Future<Either<bool, Failure>> execute(int productId) async {
    try {
      final userId = _pref.getInt(SharedPreferencesUtil.keyUserId);

      if (userId == null) {
        return Right(Failure(errMessage: 'User id not found'));
      }

      final data = await _repo.getProductCart(userId);

      final isFav = data.result
              .where((element) => element.productId == productId)
              .length >
          0;

      return Left(isFav);
    } catch (err) {
      print('CheckProductCartUseCase Error : $err');
      return Right(Failure(
        errMessage: '$err',
      ));
    }
  }
}
