using MediatR;
using System.Threading;
using System.Threading.Tasks;
using UserManagement.Common.UnitOfWork;
using UserManagement.Domain;
using UserManagement.Helper;
using UserManagement.MediatR.CommandAndQuery;
using UserManagement.Repository;

namespace UserManagement.MediatR.Handlers
{
    public class DeleteReminderCommandHandler : IRequestHandler<DeleteReminderCommand, ServiceResponse<bool>>
    {
        private readonly IReminderRepository _reminderRepository;
        private readonly IUnitOfWork<UserContext> _uow;

        public DeleteReminderCommandHandler(IReminderRepository reminderRepository,
            IUnitOfWork<UserContext> uow)
        {
            _reminderRepository = reminderRepository;
            _uow = uow;
        }

        public async Task<ServiceResponse<bool>> Handle(DeleteReminderCommand request, CancellationToken cancellationToken)
        {
            var reminder = await _reminderRepository.FindAsync(request.Id);
            if (reminder == null)
            {
                return ServiceResponse<bool>.Return404();
            }

            reminder.IsDeleted = true;
            _reminderRepository.Update(reminder);
            if (await _uow.SaveAsync() <= -1)
            {
                return ServiceResponse<bool>.Return500();
            }

            return ServiceResponse<bool>.ReturnSuccess();
        }
    }
}
