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

using MediatR;
using FnbEngine.Domain.AggregatesModel.BaristaAggregate;

namespace FnbEngine.API.Application.Queries;

/// <summary>
/// EN: Handler for getting the barista queue for a shop.
/// VI: Handler de lay hang doi barista cho shop.
/// </summary>
public class GetBaristaQueueQueryHandler : IRequestHandler<GetBaristaQueueQuery, IEnumerable<BaristaQueueItemDto>>
{
    private readonly IBaristaQueueRepository _repository;

    public GetBaristaQueueQueryHandler(IBaristaQueueRepository repository)
    {
        _repository = repository ?? throw new ArgumentNullException(nameof(repository));
    }

    public async Task<IEnumerable<BaristaQueueItemDto>> Handle(
        GetBaristaQueueQuery request, CancellationToken cancellationToken)
    {
        var items = await _repository.GetActiveByShopAsync(request.ShopId, cancellationToken);

        return items.Select(item => new BaristaQueueItemDto(
            item.Id,
            item.ShopId,
            item.OrderId,
            item.OrderItemId,
            item.DrinkName,
            item.Customizations,
            item.Priority,
            item.StatusId,
            item.StatusName,
            item.AssignedTo,
            item.EstimatedMinutes,
            item.CreatedAt,
            item.StartedAt,
            item.CompletedAt));
    }
}
