using MediatR;
using TakeoutSaaS.Application.App.Stores.Dto;
using TakeoutSaaS.Application.App.Stores.Queries;
using TakeoutSaaS.Domain.Stores.Repositories;

namespace TakeoutSaaS.Application.App.Stores.Handlers;

/// <summary>
/// 获取自提配置处理器。
/// </summary>
public sealed class GetStorePickupSettingQueryHandler(
    IStoreRepository storeRepository)
    : IRequestHandler<GetStorePickupSettingQuery, StorePickupSettingDto?>
{
    /// <inheritdoc />
    public async Task<StorePickupSettingDto?> Handle(GetStorePickupSettingQuery request, CancellationToken cancellationToken)
    {
        // 1. 读取门店并解析租户
        var store = await storeRepository.FindByIdAsync(request.StoreId, tenantId: null, cancellationToken);
        if (store is null)
        {
            return null;
        }
        var tenantId = store.TenantId;

        // 2. (空行后) 读取配置
        var setting = await storeRepository.GetPickupSettingAsync(request.StoreId, tenantId, cancellationToken);
        if (setting is null)
        {
            return null;
        }

        // 3. (空行后) 返回 DTO
        return new StorePickupSettingDto
        {
            Id = setting.Id,
            StoreId = setting.StoreId,
            AllowToday = setting.AllowToday,
            AllowDaysAhead = setting.AllowDaysAhead,
            DefaultCutoffMinutes = setting.DefaultCutoffMinutes,
            MaxQuantityPerOrder = setting.MaxQuantityPerOrder
        };
    }
}
