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

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

/// <summary>
/// 到账明细查询处理器。
/// </summary>
public sealed class GetFinanceSettlementDetailQueryHandler(
    IFinanceTransactionRepository financeTransactionRepository,
    ITenantProvider tenantProvider)
    : IRequestHandler<GetFinanceSettlementDetailQuery, FinanceSettlementDetailResultDto>
{
    /// <inheritdoc />
    public async Task<FinanceSettlementDetailResultDto> Handle(
        GetFinanceSettlementDetailQuery request,
        CancellationToken cancellationToken)
    {
        var tenantId = tenantProvider.GetCurrentTenantId();
        var rows = await financeTransactionRepository.GetSettlementDetailsAsync(
            tenantId,
            request.StoreId,
            request.ArrivedDate,
            request.PaymentMethod,
            request.Take,
            cancellationToken);

        return new FinanceSettlementDetailResultDto
        {
            Items = rows.Select(FinanceSettlementMapping.ToDetailItem).ToList()
        };
    }
}
