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

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

namespace FnbEngine.API.Application.Queries;

public class GetTicketsByShopQueryHandler : IRequestHandler<GetTicketsByShopQuery, IEnumerable<KitchenTicketDto>>
{
    private readonly IKitchenTicketRepository _repository;

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

    public async Task<IEnumerable<KitchenTicketDto>> Handle(GetTicketsByShopQuery request, CancellationToken cancellationToken)
    {
        var tickets = await _repository.GetByShopAsync(request.ShopId, request.Status, cancellationToken);

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