#nullable enable
using Baya.Application.Common;
using Baya.Application.Contracts.Search;
using Baya.Application.Models.Common;
using Baya.Application.Models.Search;
using Mediator;

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

internal sealed class SearchNursesQueryHandler(INurseSearch search)
    : IRequestHandler<SearchNursesQuery, OperationResult<PagedResult<NurseSearchResultDto>>>
{
    public async ValueTask<OperationResult<PagedResult<NurseSearchResultDto>>> Handle(SearchNursesQuery request, CancellationToken cancellationToken)
    {
        var (page, pageSize) = Pagination.Normalize(request.Page, request.PageSize);

        var criteria = new NurseSearchCriteria(
            request.ServiceCategoryId,
            request.CityId,
            request.DistrictId,
            string.IsNullOrWhiteSpace(request.NurseGender) ? null : request.NurseGender,
            request.MinPrice,
            request.MaxPrice,
            string.IsNullOrWhiteSpace(request.PriceUnit) ? null : request.PriceUnit,
            page,
            pageSize);

        var result = await search.SearchAsync(criteria, cancellationToken);
        return OperationResult<PagedResult<NurseSearchResultDto>>.SuccessResult(result);
    }
}
