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

namespace UserManagement.MediatR.Handlers
{
    public class GetOneTimeReminderQueryHandler
       : IRequestHandler<GetOneTimeReminderQuery, List<CalenderReminderDto>>
    {

        private readonly IReminderRepository _reminderRepository;
        private readonly UserInfoToken _userInfo;

        public GetOneTimeReminderQueryHandler(IReminderRepository reminderRepository, UserInfoToken userInfo)
        {
            _reminderRepository = reminderRepository;
            _userInfo = userInfo;
        }

        public async Task<List<CalenderReminderDto>> Handle(GetOneTimeReminderQuery request, CancellationToken cancellationToken)
        {
            var startDate = new DateTime(request.Year, request.Month, 1, 0, 0, 1);
            var reminders = await _reminderRepository.All
                 .Include(c => c.ReminderUsers)
                 .Where(c => c.Frequency == Frequency.OneTime
                    && c.StartDate.Month == request.Month
                    && (c.CreatedBy == Guid.Parse(_userInfo.Id) || c.ReminderUsers.Any(c => c.UserId == Guid.Parse(_userInfo.Id))))
                 .ToListAsync();

            var reminderDto = reminders.Select(c => new CalenderReminderDto
            {
                RemiderId = c.Id,
                Title = c.Subject,
                Start = c.StartDate,
                End = c.StartDate,
            }).ToList();

            return reminderDto;
        }
    }
}
