using FluentValidation;
using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1.Requests;
using PARR.Core.Repositories.Interfaces;

namespace PARR.API.Validators
{
    public class WeekendRequestValidator : AbstractValidator<WeekendRequest>
    {
        private readonly IWeekendDayRepository weekendDayService;

        public WeekendRequestValidator(IWeekendDayRepository weekendDayService)
        {
            this.weekendDayService = weekendDayService;

            RuleFor(t => t.WeekendDate).MustAsync(async (entity, value, c) => await IsEmptyAsync(entity.WeekendDate)
           ).WithMessage("Данная запись уже существует.");
        }

        private async Task<bool> IsEmptyAsync(DateOnly date)
        {
            var exist = await weekendDayService.Get().FirstOrDefaultAsync(t => t.Date == date);
            if (exist == null)
                return true;

            return false;
        }
    }
}
