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

namespace PARR.API.Validators
{
    public class DistributeRequestValidator : AbstractValidator<DistributeRequest>
    {
        public DistributeRequestValidator(IJobGroupRepository jobGroupService)
        {
            RuleFor(t => t.JobGroupId).NotEmpty().MustAsync(async (entity, value, c) =>
            {
                return await jobGroupService.Get().FirstOrDefaultAsync(t => t.Id == value) != null;
            }).WithMessage("Недопустимое значение");

            RuleFor(t => t.JobGroupId).NotEmpty().MustAsync(async (entity, value, c) =>
            {
                var jobGroup = await jobGroupService.Get()
                                        .Include(t => t.DistributionConfig)
                                        .FirstOrDefaultAsync(t => t.Id == value);

                return jobGroup != null && jobGroup.IsAutoDistributionEnabled && jobGroup.DistributionConfig != null;
            }).WithMessage("Отсутствуют настройки автораспределения");
        }
    }
}
