using FluentValidation;
using IWS5.Core.Models.Request;
using IWS5.DatabaseAccess.Interfaces;

namespace IWS5.Services.Validators;

public class ForwardWorkItemRequestValidator : AbstractValidator<ForwardWorkItemRequest>
{
    public ForwardWorkItemRequestValidator(
        IButtonRepository buttonRepository, 
        IWorkItemReferenceRepository workItemReferenceRepository)
    {
        RuleFor(x => x.WorkItemReferenceId)
            .GreaterThan(0)
            .WithMessage("WorkItemReferenceId must be greater than 0.")
            .Must(action => workItemReferenceRepository.AnyAsync(wiReference => wiReference.WorkItemReferenceId == action).Result)
            .WithMessage("WorkItemReferenceId does not exist in the work item reference table.");
        
        RuleFor(x => x.ActionFromDropDown)
            .NotEmpty()
            .WithMessage("ActionFromDropDown is required.")
            .Must(action => buttonRepository.AnyAsync(button => button.ButtonName == action).Result)
            .WithMessage("ActionFromDropDown does not exist in the buttons table.");
    }
}