// EN: Handler for GetPendingTicketsQuery.
// VI: Handler cho GetPendingTicketsQuery.

using MediatR;
using FnbEngine.Domain.AggregatesModel.KitchenAggregate;

namespace FnbEngine.API.Application.Queries;

public class GetPendingTicketsQueryHandler : IRequestHandler<GetPendingTicketsQuery, IEnumerable<KitchenTicketDto>>
{
    private readonly IKitchenTicketRepository _repository;

    public GetPendingTicketsQueryHandler(IKitchenTicketRepository repository)
    {
        _repository = repository ?? throw new ArgumentNullException(nameof(repository));
    }

    public async Task<IEnumerable<KitchenTicketDto>> Handle(GetPendingTicketsQuery request, CancellationToken cancellationToken)
    {
        var tickets = await _repository.GetPendingByStationAsync(request.Station, cancellationToken);

        return tickets.Select(t => new KitchenTicketDto(
            t.Id,
            t.SessionId,
            t.OrderItemId,
            t.ItemName,
            t.Station,
            t.Priority,
            t.Status,
            t.CreatedAt
        ));
    }
}
