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 GetFinanceSettlementAccountQueryHandler(
    IFinanceTransactionRepository financeTransactionRepository,
    ITenantProvider tenantProvider)
    : IRequestHandler<GetFinanceSettlementAccountQuery, FinanceSettlementAccountDto?>
{
    /// <inheritdoc />
    public async Task<FinanceSettlementAccountDto?> Handle(
        GetFinanceSettlementAccountQuery request,
        CancellationToken cancellationToken)
    {
        var tenantId = tenantProvider.GetCurrentTenantId();
        var snapshot = await financeTransactionRepository.GetSettlementAccountAsync(
            tenantId,
            cancellationToken);

        if (snapshot is null)
        {
            return null;
        }

        return new FinanceSettlementAccountDto
        {
            BankName = snapshot.BankName,
            BankAccountName = snapshot.BankAccountName,
            BankAccountNoMasked = snapshot.BankAccountNoMasked,
            WechatMerchantNoMasked = snapshot.WechatMerchantNoMasked,
            AlipayPidMasked = snapshot.AlipayPidMasked,
            SettlementPeriodText = snapshot.SettlementPeriodText
        };
    }
}
