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 GetStorePickupSettingQueryHandler(
    IStoreRepository storeRepository,
    ITenantProvider tenantProvider)
    : IRequestHandler<GetStorePickupSettingQuery, StorePickupSettingDto?>
{
    /// <inheritdoc />
    public async Task<StorePickupSettingDto?> Handle(GetStorePickupSettingQuery request, CancellationToken cancellationToken)
    {
        var tenantId = tenantProvider.GetCurrentTenantId();
        var setting = await storeRepository.GetPickupSettingAsync(request.StoreId, tenantId, cancellationToken);
        if (setting is null)
        {
            return null;
        }

        return new StorePickupSettingDto
        {
            Id = setting.Id,
            StoreId = setting.StoreId,
            AllowToday = setting.AllowToday,
            AllowDaysAhead = setting.AllowDaysAhead,
            DefaultCutoffMinutes = setting.DefaultCutoffMinutes,
            MaxQuantityPerOrder = setting.MaxQuantityPerOrder,
            Mode = setting.Mode,
            FineRuleJson = setting.FineRuleJson
        };
    }
}
