using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UserManagement.Data.Dto;
using UserManagement.MediatR.Queries;
using UserManagement.Repository;

namespace UserManagement.MediatR.Handlers.UserNotification
{
    public class GetUserNotificationQueryHandler : IRequestHandler<GetUserNotificationQuery, List<UserNotificationDto>>
    {
        private readonly IUserNotificationRepository _userNotificationRepository;
        private readonly UserInfoToken _userInfoToken;
        private readonly IMapper _mapper;

        public GetUserNotificationQueryHandler(
           IUserNotificationRepository userNotificationRepository,
           UserInfoToken userInfoToken,
            IMapper mapper)
        {
            _userNotificationRepository = userNotificationRepository;
            _mapper = mapper;
            _userInfoToken = userInfoToken;
        }

        public async Task<List<UserNotificationDto>> Handle(GetUserNotificationQuery request, CancellationToken cancellationToken)
        {
            var userId = Guid.Parse(_userInfoToken.Id);
            var today = DateTime.UtcNow;
            var fromDate = today.AddDays(-1).AddSeconds(1);
            var toDate = today.AddDays(1).AddSeconds(-1);
            var entities = await _userNotificationRepository
                .AllIncluding(c => c.User)
                .Where(c =>  c.UserId == userId
                && (!c.IsRead || (c.CreatedDate > fromDate && c.CreatedDate < toDate)))
                .OrderByDescending(c => c.CreatedDate)
                .Take(10).ToListAsync();
            return _mapper.Map<List<UserNotificationDto>>(entities);
        }
    }
}
