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 ListStorePickupSlotsQueryHandler(
    IStoreRepository storeRepository,
    ITenantProvider tenantProvider)
    : IRequestHandler<ListStorePickupSlotsQuery, IReadOnlyList<StorePickupSlotDto>>
{
    /// <inheritdoc />
    public async Task<IReadOnlyList<StorePickupSlotDto>> Handle(ListStorePickupSlotsQuery request, CancellationToken cancellationToken)
    {
        var tenantId = tenantProvider.GetCurrentTenantId();
        var slots = await storeRepository.GetPickupSlotsAsync(request.StoreId, tenantId, cancellationToken);
        return slots
            .Select(x => new StorePickupSlotDto
            {
                Id = x.Id,
                StoreId = x.StoreId,
                Name = x.Name,
                StartTime = x.StartTime,
                EndTime = x.EndTime,
                CutoffMinutes = x.CutoffMinutes,
                Capacity = x.Capacity,
                ReservedCount = x.ReservedCount,
                Weekdays = x.Weekdays,
                IsEnabled = x.IsEnabled
            })
            .ToList();
    }
}
