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

namespace IWS5.Services.Validators;

public class UpdateDocumentContentRequestValidator : AbstractValidator<UpdateDocumentContentRequest> {
    const int MaxFileSizeInBytes = 25 * 1024 * 1024; // 25 MB
    public UpdateDocumentContentRequestValidator(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.WorkItemReferenceId)
            .GreaterThan(0).WithMessage("WorkItemReferenceId is required and must be greater than 0.")
            .Must(workItemReferenceId => workItemReferenceRepository.AnyAsync(workItemReference => workItemReference.WorkItemReferenceId == workItemReferenceId).Result)
            .WithMessage("WorkItemReferenceId must be among existing [dbo].[ATWorkItemReference].WorkItemReferenceId 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.DocumentFile)
            .NotNull().NotEmpty().WithMessage("Document file is required.")
            .Must(file => file.Length <= MaxFileSizeInBytes)
            .WithMessage($"Document file size must not exceed {MaxFileSizeInBytes} MB.");
    }
}