using Application.Services.Database;
using FluentValidation;

namespace Application.Endpoints.MedicalHistories;

public class MedicalHistoryUpdateValidation : AbstractValidator<MedicalHistoryUpdateDto>
{
    private readonly IMedicalHistoryRepository _medicalHistoryRepository;
    private readonly IPatientRepository _patientRepository;

    public MedicalHistoryUpdateValidation(IMedicalHistoryRepository medicalHistoryRepository,
        IPatientRepository patientRepository)
    {
        _medicalHistoryRepository = medicalHistoryRepository;
        _patientRepository = patientRepository;

        RuleFor(x => x.Id)
            .NotEmpty().WithMessage("Id is required")
            .MustAsync(BeExistingMedicalHistoryRecord).WithMessage("Medical history record does not exist");

        RuleFor(x => x.Content)
            .NotEmpty().WithMessage("Description is required.");
    }

    private async Task<bool> BeExistingMedicalHistoryRecord(Guid guid, CancellationToken token)
    {
        var record = await _medicalHistoryRepository.GetByIdAsync(guid);
        return record != null;
    }
}