using MediatR;
using TakeoutSaaS.Application.App.Inventory.Dto;
using TakeoutSaaS.Application.App.Inventory.Queries;
using TakeoutSaaS.Domain.Inventory.Repositories;
using TakeoutSaaS.Shared.Abstractions.Tenancy;

namespace TakeoutSaaS.Application.App.Inventory.Handlers;

/// <summary>
/// 查询库存处理器。
/// </summary>
public sealed class GetInventoryItemQueryHandler(
    IInventoryRepository inventoryRepository,
    ITenantProvider tenantProvider)
    : IRequestHandler<GetInventoryItemQuery, InventoryItemDto?>
{
    /// <inheritdoc />
    public async Task<InventoryItemDto?> Handle(GetInventoryItemQuery request, CancellationToken cancellationToken)
    {
        // 1. 读取库存
        var tenantId = tenantProvider.GetCurrentTenantId();
        var item = await inventoryRepository.FindBySkuAsync(tenantId, request.StoreId, request.ProductSkuId, cancellationToken);
        // 2. 返回 DTO
        return item is null ? null : InventoryMapping.ToDto(item);
    }
}
