using MediatR;
using TakeoutSaaS.Application.App.Billings.Dto;
using TakeoutSaaS.Application.App.Billings.Queries;
using TakeoutSaaS.Domain.Tenants.Repositories;

namespace TakeoutSaaS.Application.App.Billings.Handlers;

/// <summary>
/// 获取租户支付记录查询处理器。
/// </summary>
public sealed class GetTenantPaymentsQueryHandler(ITenantPaymentRepository paymentRepository)
    : IRequestHandler<GetTenantPaymentsQuery, List<PaymentDto>>
{
    /// <summary>
    /// 处理获取支付记录请求。
    /// </summary>
    /// <param name="request">查询请求。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>支付记录列表。</returns>
    public async Task<List<PaymentDto>> Handle(GetTenantPaymentsQuery request, CancellationToken cancellationToken)
    {
        // 1. 查询支付记录
        var payments = await paymentRepository.GetByBillingIdAsync(request.BillId, cancellationToken);

        // 2. 映射并返回 DTO
        return payments.Select(p => p.ToDto()).ToList();
    }
}
