using MediatR;
using TakeoutSaaS.Application.Identity.Contracts;
using TakeoutSaaS.Application.Identity.Queries;
using TakeoutSaaS.Domain.Identity.Enums;
using TakeoutSaaS.Domain.Identity.Repositories;

namespace TakeoutSaaS.Application.Identity.Handlers;

/// <summary>
/// 菜单详情查询处理器。
/// </summary>
public sealed class MenuDetailQueryHandler(
    IMenuRepository menuRepository)
    : IRequestHandler<MenuDetailQuery, MenuDefinitionDto?>
{
    /// <inheritdoc />
    public async Task<MenuDefinitionDto?> Handle(MenuDetailQuery request, CancellationToken cancellationToken)
    {
        // 1. 固定读取管理端菜单
        var portal = PortalType.Admin;

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

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