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 ListMenusQueryHandler(
    IMenuRepository menuRepository,
    ITenantProvider tenantProvider)
    : IRequestHandler<ListMenusQuery, IReadOnlyList<MenuDefinitionDto>>
{
    /// <inheritdoc />
    public async Task<IReadOnlyList<MenuDefinitionDto>> Handle(ListMenusQuery request, CancellationToken cancellationToken)
    {
        // 1. 获取租户
        var tenantId = tenantProvider.GetCurrentTenantId();

        // 2. 查询列表
        var entities = await menuRepository.GetByTenantAsync(tenantId, cancellationToken);

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

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