import 'package:dartz/dartz.dart';
import 'package:injectable/injectable.dart';

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

@injectable
class AddProductCartUseCase {
  @factoryParam
  final SharedPreferencesUtil _pref;
  final ProductRepository _productRepo;

  AddProductCartUseCase(this._productRepo, this._pref);

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

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

      final data =
          await _productRepo.addProductCart(userId, productId, storeId);

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