using System.Linq;
using MediatR;
using TakeoutSaaS.Application.App.Stores.Dto;
using TakeoutSaaS.Application.App.Stores.Queries;
using TakeoutSaaS.Domain.Stores.Repositories;
using TakeoutSaaS.Shared.Abstractions.Tenancy;

namespace TakeoutSaaS.Application.App.Stores.Handlers;

/// <summary>
/// 桌台区域列表查询处理器。
/// </summary>
public sealed class ListStoreTableAreasQueryHandler(IStoreRepository storeRepository, ITenantProvider tenantProvider)
    : IRequestHandler<ListStoreTableAreasQuery, IReadOnlyList<StoreTableAreaDto>>
{
    /// <inheritdoc />
    public async Task<IReadOnlyList<StoreTableAreaDto>> Handle(ListStoreTableAreasQuery request, CancellationToken cancellationToken)
    {
        // 1. 查询区域列表
        var tenantId = tenantProvider.GetCurrentTenantId();
        var areas = await storeRepository.GetTableAreasAsync(request.StoreId, tenantId, cancellationToken);

        // 2. 映射 DTO
        return areas.Select(StoreMapping.ToDto).ToList();
    }
}
