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 AddProductFavoriteUseCase {
  @factoryParam
  final SharedPreferencesUtil _pref;
  final ProductRepository _productRepo;

  AddProductFavoriteUseCase(this._productRepo, 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 _productRepo.addProductFavorite(userId, productId);

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