using MediatR;
using TakeoutSaaS.Application.App.Finance.Transactions.Dto;
using TakeoutSaaS.Application.App.Finance.Transactions.Queries;
using TakeoutSaaS.Domain.Finance.Repositories;
using TakeoutSaaS.Shared.Abstractions.Tenancy;

namespace TakeoutSaaS.Application.App.Finance.Transactions.Handlers;

/// <summary>
/// 交易流水详情查询处理器。
/// </summary>
public sealed class GetFinanceTransactionDetailQueryHandler(
    IFinanceTransactionRepository financeTransactionRepository,
    ITenantProvider tenantProvider)
    : IRequestHandler<GetFinanceTransactionDetailQuery, FinanceTransactionDetailDto?>
{
    /// <inheritdoc />
    public async Task<FinanceTransactionDetailDto?> Handle(GetFinanceTransactionDetailQuery request, CancellationToken cancellationToken)
    {
        // 1. 读取租户上下文并查询详情。
        var tenantId = tenantProvider.GetCurrentTenantId();
        var record = await financeTransactionRepository.GetDetailAsync(
            tenantId,
            request.StoreId,
            request.SourceType,
            request.SourceId,
            cancellationToken);

        // 2. 映射详情输出。
        return record is null ? null : FinanceTransactionMapping.ToDetail(record);
    }
}
