package com.jeesharp.business.modules.pay.payfundbalance.convert;

import cn.hutool.json.JSONObject;
import com.jeesharp.business.modules.pay.payfundbalance.enums.PayFundAccountTypeEnum;
import com.jeesharp.business.modules.pay.payfundbalance.vo.PayFundBalanceItemVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

/**
 * 微信余额 JSON 与展示 VO 的转换。
 *
 * @author jeesharp
 * @since 2026-06-12
 */
@Mapper
public interface PayFundBalanceConvert {

    PayFundBalanceConvert INSTANCE = Mappers.getMapper(PayFundBalanceConvert.class);

    default PayFundBalanceItemVO toItemVO(String accountType, JSONObject wx) {
        PayFundBalanceItemVO vo = new PayFundBalanceItemVO();
        vo.setAccountType(accountType);
        vo.setAccountTypeName(PayFundAccountTypeEnum.getDescByValue(accountType));
        vo.setSuccess(Boolean.TRUE);
        if (wx != null) {
            vo.setAvailableAmount(wx.getLong("available_amount"));
            vo.setPendingAmount(wx.getLong("pending_amount"));
        }
        return vo;
    }

    default PayFundBalanceItemVO toFailedItem(String accountType, String errorMessage) {
        PayFundBalanceItemVO vo = new PayFundBalanceItemVO();
        vo.setAccountType(accountType);
        vo.setAccountTypeName(PayFundAccountTypeEnum.getDescByValue(accountType));
        vo.setSuccess(Boolean.FALSE);
        vo.setErrorMessage(errorMessage);
        return vo;
    }
}
