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

namespace UserManagement.MediatR.Handlers
{
    public class DeletePageHelperCommandHandler : IRequestHandler<DeletePageHelperCommand, ServiceResponse<bool>>
    {
        private readonly IPageHelperRepository _pageHelperRepository;
        private readonly IUnitOfWork<UserContext> _uow;

        public DeletePageHelperCommandHandler(
           IPageHelperRepository pageHelperRepository,
            IUnitOfWork<UserContext> uow)
        {
            _pageHelperRepository = pageHelperRepository;
            _uow = uow;
        }
        public async Task<ServiceResponse<bool>> Handle(DeletePageHelperCommand request, CancellationToken cancellationToken)
        {
            var entityExist = await _pageHelperRepository.FindAsync(request.Id);

            if (entityExist == null)
            {
                return ServiceResponse<bool>.Return404();
            }

            _pageHelperRepository.Delete(request.Id);

            if (await _uow.SaveAsync() <= 0)
            {
                return ServiceResponse<bool>.Return500();
            }
            return ServiceResponse<bool>.ReturnSuccess();
        }
    }
}
