using Baya.Domain.Entities.Catalog;
using FluentValidation;

namespace Baya.Application.Features.Search.Queries.SearchNurses;

public sealed class SearchNursesQueryValidator : AbstractValidator<SearchNursesQuery>
{
    public SearchNursesQueryValidator()
    {
        RuleFor(x => x.ServiceCategoryId).GreaterThan(0);
        RuleFor(x => x.CityId).GreaterThan(0);
        RuleFor(x => x.DistrictId).GreaterThan(0).When(x => x.DistrictId.HasValue);

        // Same-gender matching is a first-class facet — when present it must be an exact known value.
        RuleFor(x => x.NurseGender)
            .Must(g => g is "male" or "female")
            .When(x => !string.IsNullOrWhiteSpace(x.NurseGender))
            .WithMessage("nurse_gender must be 'male' or 'female'.");

        RuleFor(x => x.MinPrice).GreaterThanOrEqualTo(0).When(x => x.MinPrice.HasValue);
        RuleFor(x => x.MaxPrice).GreaterThanOrEqualTo(0).When(x => x.MaxPrice.HasValue);
        RuleFor(x => x)
            .Must(x => x.MinPrice <= x.MaxPrice)
            .When(x => x.MinPrice.HasValue && x.MaxPrice.HasValue)
            .WithMessage("min_price must be less than or equal to max_price.");

        RuleFor(x => x.PriceUnit)
            .Must(PriceUnits.IsValid)
            .When(x => !string.IsNullOrWhiteSpace(x.PriceUnit))
            .WithMessage("price_unit must be one of: per_hour, per_session, per_half_day, per_day, per_24h.");

        RuleFor(x => x.PageSize).LessThanOrEqualTo(Baya.Application.Common.Pagination.MaxPageSize);
    }
}
