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 GetFinanceSettlementStatsQueryHandler(
    IFinanceTransactionRepository financeTransactionRepository,
    ITenantProvider tenantProvider)
    : IRequestHandler<GetFinanceSettlementStatsQuery, FinanceSettlementStatsDto>
{
    /// <inheritdoc />
    public async Task<FinanceSettlementStatsDto> Handle(
        GetFinanceSettlementStatsQuery request,
        CancellationToken cancellationToken)
    {
        var tenantId = tenantProvider.GetCurrentTenantId();
        var snapshot = await financeTransactionRepository.GetSettlementStatsAsync(
            tenantId,
            request.StoreId,
            DateTime.UtcNow,
            cancellationToken);

        return new FinanceSettlementStatsDto
        {
            TodayArrivedAmount = snapshot.TodayArrivedAmount,
            YesterdayArrivedAmount = snapshot.YesterdayArrivedAmount,
            CurrentMonthArrivedAmount = snapshot.CurrentMonthArrivedAmount,
            CurrentMonthTransactionCount = snapshot.CurrentMonthTransactionCount
        };
    }
}
