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

using MediatR;
using FnbEngine.Domain.AggregatesModel.TableAggregate;

namespace FnbEngine.API.Application.Queries;

/// <summary>
/// EN: Handler for getting tables by shop.
/// VI: Handler lấy danh sách bàn theo shop.
/// </summary>
public class GetTablesQueryHandler : IRequestHandler<GetTablesQuery, IEnumerable<TableDto>>
{
    private readonly ITableRepository _tableRepository;

    public GetTablesQueryHandler(ITableRepository tableRepository)
    {
        _tableRepository = tableRepository ?? throw new ArgumentNullException(nameof(tableRepository));
    }

    public async Task<IEnumerable<TableDto>> Handle(GetTablesQuery request, CancellationToken cancellationToken)
    {
        var tables = await _tableRepository.GetByShopAsync(request.ShopId, cancellationToken);

        return tables.Select(t => new TableDto(
            t.Id,
            t.ShopId,
            t.TableNumber,
            t.Capacity,
            t.Zone,
            t.Status.Name
        ));
    }
}
