using System.Linq;
using MediatR;
using TakeoutSaaS.Application.App.Stores;
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 ListStoreBusinessHoursQueryHandler(
    IStoreRepository storeRepository,
    ITenantProvider tenantProvider)
    : IRequestHandler<ListStoreBusinessHoursQuery, IReadOnlyList<StoreBusinessHourDto>>
{
    private readonly IStoreRepository _storeRepository = storeRepository;
    private readonly ITenantProvider _tenantProvider = tenantProvider;

    /// <inheritdoc />
    public async Task<IReadOnlyList<StoreBusinessHourDto>> Handle(ListStoreBusinessHoursQuery request, CancellationToken cancellationToken)
    {
        // 1. 查询时段列表
        var tenantId = _tenantProvider.GetCurrentTenantId();
        var hours = await _storeRepository.GetBusinessHoursAsync(request.StoreId, tenantId, cancellationToken);

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