import 'package:boilerplate/data/models/notification/save_fcm_token_req_model.dart';
import 'package:boilerplate/data/repositories/notification_repository.dart';
import 'package:dartz/dartz.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:injectable/injectable.dart';

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

@injectable
class SaveFCMTokenUseCase {
  final NotificationRepository _repo;

  SaveFCMTokenUseCase(this._repo);

  Future<Either<bool, Failure>> execute(int userId) async {
    try {
      final token = await FirebaseMessaging.instance.getToken();

      final data = await _repo.saveFCMToken(SaveFCMTokenReqModel(
        token: token ?? '',
        userId: userId,
      ));

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