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

namespace TakeoutSaaS.Application.App.Tenants.Handlers;

/// <summary>
/// 账单详情查询处理器。
/// </summary>
public sealed class GetTenantBillQueryHandler(ITenantBillingRepository billingRepository)
    : IRequestHandler<GetTenantBillQuery, TenantBillingDto?>
{
    /// <summary>
    /// 查询账单详情。
    /// </summary>
    /// <param name="request">查询请求。</param>
    /// <param name="cancellationToken">取消标记。</param>
    /// <returns>账单 DTO 或 null。</returns>
    public async Task<TenantBillingDto?> Handle(GetTenantBillQuery request, CancellationToken cancellationToken)
    {
        // 1. 查询账单
        var bill = await billingRepository.FindByIdAsync(request.TenantId, request.BillingId, cancellationToken);

        // 2. 返回 DTO 或 null
        return bill?.ToDto();
    }
}
