package com.jeesharp.business.modules.finance.application.api;

import com.jeesharp.business.modules.finance.api.PayOrderApi;
import com.jeesharp.business.modules.finance.payorder.application.PayOrderApplicationService;
import com.jeesharp.business.modules.finance.payorder.vo.PayOrderInfoVO;
import com.jeesharp.business.modules.finance.payorder.vo.PayOrderIntentVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * {@link PayOrderApi} 跨模块门面实现：薄委托层，所有用例编排均在 {@link PayOrderApplicationService}。
 *
 * <p>调用方（如 mobile-gateway 预支付、微信回调适配层）只依赖 finance-api，不直接依赖 Application 模块，
 * 保证依赖方向：垂直域 / 网关 → finance-api → 本类 → PayOrderApplicationService。</p>
 *
 * @author jeesharp
 * @since 2026-06-09
 */
@Service
@AllArgsConstructor
public class PayOrderApiImpl implements PayOrderApi {

    private final PayOrderApplicationService payOrderApplicationService;

    /** 按主键查询支付单详情（管理端 / 对账只读）。 */
    @Override
    public PayOrderInfoVO get(String id) {
        return payOrderApplicationService.get(id);
    }

    /**
     * 创建 JSAPI 支付意图（最简入参：业务场景 + 业务单 + 金额 + 会员 + 标题）。
     *
     * @return payOrderId、out_trade_no，供 {@code WechatJsapiPayOrderPrepayService} 统一下单
     */
    @Override
    public PayOrderIntentVO createIntentForJsapi(String bizScene, String bizOrderId, BigDecimal amountYuan,
            String memberId, String subject) {
        return payOrderApplicationService.createIntentForJsapi(bizScene, bizOrderId, amountYuan, memberId, subject);
    }

    /** 创建 JSAPI 支付意图（补充描述、回调 URL、openid、客户端 IP）。 */
    @Override
    public PayOrderIntentVO createIntentForJsapi(String bizScene, String bizOrderId, BigDecimal amountYuan,
            String memberId, String subject, String description, String notifyUrl, String channelUserId,
            String userIp) {
        return payOrderApplicationService.createIntentForJsapi(bizScene, bizOrderId, amountYuan, memberId, subject,
                description, notifyUrl, channelUserId, userIp);
    }

    /** 创建 JSAPI 支付意图（补充租户 ID，多租户支付配置隔离）。 */
    @Override
    public PayOrderIntentVO createIntentForJsapi(String bizScene, String bizOrderId, BigDecimal amountYuan,
            String memberId, String subject, String description, String notifyUrl, String channelUserId,
            String userIp, String tenantId) {
        return payOrderApplicationService.createIntentForJsapi(bizScene, bizOrderId, amountYuan, memberId, subject,
                description, notifyUrl, channelUserId, userIp, tenantId);
    }

    /** 创建 JSAPI 支付意图（补充结算模式、是否分账，供商户结算 / 收付通分账链路）。 */
    @Override
    public PayOrderIntentVO createIntentForJsapi(String bizScene, String bizOrderId, BigDecimal amountYuan,
            String memberId, String subject, String description, String notifyUrl, String channelUserId,
            String userIp, String tenantId, String settleMode, Boolean needProfitSharing) {
        return payOrderApplicationService.createIntentForJsapi(bizScene, bizOrderId, amountYuan, memberId, subject,
                description, notifyUrl, channelUserId, userIp, tenantId, settleMode, needProfitSharing);
    }

    /**
     * 创建 JSAPI 支付意图（完整参数：含收付通路由 DIRECT / PARTNER / COMBINE）。
     * <p>各业态预支付入口最终应调用此重载，以便写入 subMchId、combineOutTradeNo 等路由字段。</p>
     */
    @Override
    public PayOrderIntentVO createIntentForJsapi(String bizScene, String bizOrderId, BigDecimal amountYuan,
            String memberId, String subject, String description, String notifyUrl, String channelUserId,
            String userIp, String tenantId, String settleMode, Boolean needProfitSharing,
            com.jeesharp.business.modules.finance.payorder.dto.PayOrderPrepayRouteDTO route) {
        return payOrderApplicationService.createIntentForJsapi(bizScene, bizOrderId, amountYuan, memberId, subject,
                description, notifyUrl, channelUserId, userIp, tenantId, settleMode, needProfitSharing, route);
    }

    /**
     * 微信统一下单成功后回写 prepay_id，支付单状态 INIT → PREPAY_CREATED。
     * 由预支付服务在拿到 prepay 参数后调用。
     */
    @Override
    public void bindPrepayAfterJsapi(String payOrderId, String prepayIdOrPackage) {
        payOrderApplicationService.bindPrepayAfterJsapi(payOrderId, prepayIdOrPackage);
    }

    /**
     * 微信回调成功：按商户 out_trade_no（或合单 combine_out_trade_no）将支付单置为 SUCCESS。
     * 由 {@code WechatPayNotifyAdapterService#recordFinancePayNotify} 在无 payOrderId 时调用（兼容旧链路）。
     */
    @Override
    public void markPaidByWechatOutTradeNo(String outTradeNo, String thirdPartyTradeNo, BigDecimal amountYuan,
            LocalDateTime payTime) {
        payOrderApplicationService.markPaidByWechatOutTradeNo(outTradeNo, thirdPartyTradeNo, amountYuan, payTime);
    }

    /**
     * 微信回调成功：按 attach.payOrderId 精确定位支付单并置为 SUCCESS（新链路首选）。
     * 由 {@code WechatPayNotifyAdapterService#recordFinancePayNotify} 在 attach 带 payOrderId 时调用。
     */
    @Override
    public void markPaidByPayOrderId(String payOrderId, String thirdPartyTradeNo, BigDecimal amountYuan,
            LocalDateTime payTime) {
        payOrderApplicationService.markPaidByPayOrderId(payOrderId, thirdPartyTradeNo, amountYuan, payTime);
    }
}
