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

namespace TakeoutSaaS.Application.App.Merchants.Handlers;

/// <summary>
/// 列出类目。
/// </summary>
public sealed class ListMerchantCategoriesQueryHandler(
    IMerchantCategoryRepository categoryRepository)
    : IRequestHandler<ListMerchantCategoriesQuery, IReadOnlyList<MerchantCategoryDto>>
{
    /// <summary>
    /// 列出所有类目。
    /// </summary>
    /// <param name="request">查询请求。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>类目 DTO 列表。</returns>
    public async Task<IReadOnlyList<MerchantCategoryDto>> Handle(ListMerchantCategoriesQuery request, CancellationToken cancellationToken)
    {
        // 1. 查询类目列表
        var categories = await categoryRepository.ListAsync(request.TenantId, cancellationToken);

        // 2. (空行后) 映射 DTO
        return MerchantMapping.ToCategoryDtos(categories);
    }
}
