using FluentValidation;
using PARR.API.Contracts.V1.Requests;
using PARR.Core.Repositories.Interfaces;
using PARR.Core.Repositories.Interfaces.Job;
using PARR.Domain.Enums;

namespace PARR.API.Validators
{
    public class TemplateActivatorRequestValidator : AbstractValidator<TemplateActivatorRequest>
    {
        public TemplateActivatorRequestValidator(
                ITemplateRepository templateService,
                IJobRepository jobService,
                IJobGroupRepository jobGroupService
            )
        {
            RuleFor(t => t.EntityType).Must(type =>
                 Enum.IsDefined(typeof(SyncTaskEntityTypeEnum), type)
            ).WithMessage("Некорректное значение EntityType");

            RuleFor(t => t.EsppObject).Must(obj =>
                Enum.IsDefined(typeof(TemplateActivatorEsppObjectTypesEnum), obj)
            ).WithMessage("Некорректное значение EsppObject");

            RuleFor(t => t.Id)
                .MustAsync(async (entity, value, c) =>
                {
                    switch (entity.EntityType)
                    {
                        case SyncTaskEntityTypeEnum.JobGroup:
                            return await jobGroupService.GetAsync(value) != null;
                        case SyncTaskEntityTypeEnum.Job:
                            return await jobService.GetAsync(value) != null;
                        case SyncTaskEntityTypeEnum.Template:
                            return await templateService.GetAsync(value) != null;
                    }

                    return false;
                })
                .WithMessage("Некорректное значение Id");
        }
    }
}
