using MediatR;
using TakeoutSaaS.Application.Identity.Contracts;
using TakeoutSaaS.Application.Identity.Queries;
using TakeoutSaaS.Domain.Identity.Repositories;
using TakeoutSaaS.Shared.Abstractions.Tenancy;

namespace TakeoutSaaS.Application.Identity.Handlers;

/// <summary>
/// 菜单详情查询处理器。
/// </summary>
public sealed class MenuDetailQueryHandler(
    IMenuRepository menuRepository,
    ITenantProvider tenantProvider)
    : IRequestHandler<MenuDetailQuery, MenuDefinitionDto?>
{
    /// <inheritdoc />
    public async Task<MenuDefinitionDto?> Handle(MenuDetailQuery request, CancellationToken cancellationToken)
    {
        // 1. 获取租户
        var tenantId = tenantProvider.GetCurrentTenantId();

        // 2. 查询实体
        var entity = await menuRepository.FindByIdAsync(request.Id, tenantId, cancellationToken);
        if (entity is null)
        {
            return null;
        }

        // 3. 映射并返回
        return MenuMapper.ToDto(entity);
    }
}
