using System.Globalization;
using TakeoutSaaS.Application.App.Finance.Settlement.Dto;
using TakeoutSaaS.Domain.Finance.Models;
using TakeoutSaaS.Domain.Payments.Enums;

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

/// <summary>
/// 到账查询映射辅助。
/// </summary>
internal static class FinanceSettlementMapping
{
    /// <summary>
    /// 支付方式转渠道编码。
    /// </summary>
    public static string ToChannelCode(PaymentMethod paymentMethod)
    {
        return paymentMethod switch
        {
            PaymentMethod.WeChatPay => "wechat",
            PaymentMethod.Alipay => "alipay",
            _ => "unknown"
        };
    }

    /// <summary>
    /// 支付方式转渠道文案。
    /// </summary>
    public static string ToChannelText(PaymentMethod paymentMethod)
    {
        return paymentMethod switch
        {
            PaymentMethod.WeChatPay => "微信支付",
            PaymentMethod.Alipay => "支付宝",
            _ => "未知渠道"
        };
    }

    /// <summary>
    /// 映射到账汇总行。
    /// </summary>
    public static FinanceSettlementListItemDto ToListItem(FinanceSettlementListItemSnapshot source)
    {
        return new FinanceSettlementListItemDto
        {
            ArrivedDate = source.ArrivedDate,
            Channel = ToChannelCode(source.PaymentMethod),
            ChannelText = ToChannelText(source.PaymentMethod),
            TransactionCount = source.TransactionCount,
            ArrivedAmount = decimal.Round(source.ArrivedAmount, 2, MidpointRounding.AwayFromZero)
        };
    }

    /// <summary>
    /// 映射到账明细行。
    /// </summary>
    public static FinanceSettlementDetailItemDto ToDetailItem(FinanceSettlementDetailItemSnapshot source)
    {
        return new FinanceSettlementDetailItemDto
        {
            OrderNo = source.OrderNo,
            Amount = decimal.Round(source.Amount, 2, MidpointRounding.AwayFromZero),
            PaidAt = source.PaidAt
        };
    }

    /// <summary>
    /// 格式化金额（导出场景）。
    /// </summary>
    public static string FormatAmount(decimal value)
    {
        return decimal.Round(value, 2, MidpointRounding.AwayFromZero)
            .ToString("0.00", CultureInfo.InvariantCulture);
    }
}
