import 'package:boilerplate/core/utils/shared_preferences_util.dart';
import 'package:boilerplate/data/repositories/notification_repository.dart';
import 'package:boilerplate/domain/entities/notification/notification_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 GetNotificationsUseCase {
  final NotificationRepository _repo;
  @factoryParam
  final SharedPreferencesUtil _pref;

  GetNotificationsUseCase(this._repo, this._pref);

  Future<Either<List<NotificationData>, 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.getNotifications(userId);

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