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 ListMenusQueryHandler(IMenuRepository menuRepository)
    : IRequestHandler<ListMenusQuery, IReadOnlyList<MenuDefinitionDto>>
{
    /// <inheritdoc />
    public async Task<IReadOnlyList<MenuDefinitionDto>> Handle(ListMenusQuery request, CancellationToken cancellationToken)
    {
        // 1. 查询租户端菜单
        var entities = await menuRepository.GetByPortalAsync(PortalType.Tenant, cancellationToken);

        // 2. 映射 DTO
        var items = entities.Select(MenuMapper.ToDto).ToList();

        // 3. 返回结果
        return items;
    }
}
