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 ListStoreHolidaysQueryHandler(
    IStoreRepository storeRepository,
    ITenantProvider tenantProvider)
    : IRequestHandler<ListStoreHolidaysQuery, IReadOnlyList<StoreHolidayDto>>
{
    private readonly IStoreRepository _storeRepository = storeRepository;
    private readonly ITenantProvider _tenantProvider = tenantProvider;

    /// <inheritdoc />
    public async Task<IReadOnlyList<StoreHolidayDto>> Handle(ListStoreHolidaysQuery request, CancellationToken cancellationToken)
    {
        // 1. 查询节假日
        var tenantId = _tenantProvider.GetCurrentTenantId();
        var holidays = await _storeRepository.GetHolidaysAsync(request.StoreId, tenantId, cancellationToken);

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