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

namespace Baya.Application.Features.Messaging.Queries.ListTicketsForAdmin;

internal sealed class ListTicketsForAdminQueryHandler(IUnitOfWork unitOfWork)
    : IRequestHandler<ListTicketsForAdminQuery, OperationResult<PagedResult<TicketSummaryDto>>>
{
    public async ValueTask<OperationResult<PagedResult<TicketSummaryDto>>> Handle(ListTicketsForAdminQuery request, CancellationToken cancellationToken)
    {
        var (page, pageSize) = Pagination.Normalize(request.Page, request.PageSize);
        var result = await unitOfWork.TicketRepository.ListForAdminAsync(
            request.Status, request.Category, request.ReferenceCode, request.BookingId, request.RefundId, page, pageSize, cancellationToken);
        return OperationResult<PagedResult<TicketSummaryDto>>.SuccessResult(result);
    }
}
