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

namespace IWS5.Services.Validators;

public class CreateWorkItemRequestValidator: AbstractValidator<CreateWorkItemRequest>
{
    public CreateWorkItemRequestValidator(IProcessRepository processRepository, IDocumentCabinetRepository documentCabinetRepository, IFieldDefinitionRepository fieldDefinitionRepository)
    { 
        RuleFor(x => x.WorkItemName)
            .NotNull().WithMessage("WorkItemName must not be null.")
            .NotEmpty().WithMessage("WorkItemName must not be empty.")
            .MaximumLength(255).WithMessage("WorkItemName must not exceed 255 characters.");

        RuleFor(x => x.ProcessId).GreaterThan(0).WithMessage("ProcessId is required")
            .Must(processId => processRepository.AnyAsync(p => p.ProcessId == processId).Result)
            .WithMessage("Process with the specified ID does not exist.");
        
        RuleForEach(x => x.FolderFields)
            .SetValidator(new CreateFolderFieldValueRequestValidator(fieldDefinitionRepository));

        RuleFor(x=>x.Documents)
            .ForEach(document => document
                .SetValidator(new CreateDocumentRequestValidator(documentCabinetRepository)));
    }
}
