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

namespace IWS5.Services.Validators {
    public class RenameDocumentRequestValidator : AbstractValidator<RenameDocumentRequest> {
        public RenameDocumentRequestValidator(IProcessRepository processRepository, IRightsRepository rightsRepository, IDocumentRepository documentRepository, IWorkItemReferenceRepository workItemReferenceRepository) {
            RuleFor(x => x.ProcessId)
                .GreaterThan(0).WithMessage("ProcessId is required and must be greater than 0.")
                .Must(processId => processRepository.AnyAsync(process => process.ProcessId == processId).Result)
                .WithMessage("ProcessId must be among existing [dbo].[ATProcessDefinition].ProcessID records.");

            RuleFor(x => x.DocumentId)
                .GreaterThan(0).WithMessage("DocumentId is required and must be greater than 0.")
                .Must(documentId => documentRepository.AnyAsync(document => document.DocumentId == documentId).Result)
                .WithMessage("DocumentId must be among existing [dbo].[ATDocument].DocumentId records.");

            RuleFor(x => x.NewName)
                .NotNull().NotEmpty().WithMessage("New document name is required.")
                .MaximumLength(255).WithMessage("Document name must not exceed 255 characters.");
        }
    }
}
