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

namespace TakeoutSaaS.Application.App.Products.Handlers;

/// <summary>
/// 商品详情查询处理器。
/// </summary>
public sealed class GetProductByIdQueryHandler(
    IProductRepository productRepository)
    : IRequestHandler<GetProductByIdQuery, ProductDto?>
{
    /// <inheritdoc />
    public async Task<ProductDto?> Handle(GetProductByIdQuery request, CancellationToken cancellationToken)
    {
        // 1. 查询商品（跨租户）
        var product = await productRepository.FindByIdAsync(request.ProductId, cancellationToken);
        // 2. (空行后) 映射并返回
        return product == null ? null : ProductMapping.ToDto(product);
    }
}
