import 'package:boilerplate/core/utils/shared_preferences_util.dart';
import 'package:boilerplate/data/repositories/product_repository.dart';
import 'package:boilerplate/domain/entities/product/product_favorite_data.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 GetProductFavoritesUseCase {
  final ProductRepository _repo;
  @factoryParam
  final SharedPreferencesUtil _pref;

  GetProductFavoritesUseCase(this._repo, this._pref);

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

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

      final data = await _repo.getProductFavorites(userId);

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