package com.jeesharp.business.modules.mall.application.support;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.finance.api.MerchantSettleConfigApi;
import com.jeesharp.business.modules.finance.merchantsettle.enums.SettleModeEnum;
import com.jeesharp.business.modules.finance.merchantsettle.vo.MerchantSettleConfigSnapshotVO;
import com.jeesharp.business.modules.mall.vo.MallSettleShelfHintVO;
import com.jeesharp.business.modules.pay.api.PaySubMerchantOnboardingApi;
import com.jeesharp.framework.exception.BizErrorException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 商城商品上架前的结算进件守卫：仅当商户启用微信分账结算时，要求已完成特约商户进件。
 *
 * @author jeesharp
 * @since 2026-06-11
 */
@Component
@AllArgsConstructor
public class MallSettleOnboardingGuard {

    private final MerchantSettleConfigApi merchantSettleConfigApi;
    private final PaySubMerchantOnboardingApi paySubMerchantOnboardingApi;

    /**
     * 商品即将上架时校验：分账商户须已有有效 {@code sub_mch_id}。
     *
     * @param merchantTenantId 商品所属商户租户主键
     */
    public void assertCanShelf(String merchantTenantId) {
        MallSettleShelfHintVO hint = evaluateShelfHint(merchantTenantId);
        if (!Boolean.TRUE.equals(hint.getCanShelf())) {
            throw new BizErrorException(StrUtil.blankToDefault(hint.getMessage(), "请先完成微信支付商户进件"));
        }
    }

    /**
     * 评估商户上架前的结算模式与进件状态（不抛异常，供编辑页展示）。
     */
    public MallSettleShelfHintVO evaluateShelfHint(String merchantTenantId) {
        MallSettleShelfHintVO vo = new MallSettleShelfHintVO();
        vo.setMerchantTenantId(merchantTenantId);
        if (StrUtil.isBlank(merchantTenantId)) {
            vo.setCanShelf(false);
            vo.setMessage("无法识别商户租户，请重新登录后再试");
            return vo;
        }
        MerchantSettleConfigSnapshotVO snapshot = merchantSettleConfigApi.resolveSnapshot(merchantTenantId.trim());
        vo.setSettleMode(snapshot.getSettleMode());
        vo.setSettleModeName(SettleModeEnum.getDescByValue(snapshot.getSettleMode()));
        vo.setCommissionRate(snapshot.getCommissionRate());
        vo.setNeedProfitSharing(snapshot.getNeedProfitSharing());
        vo.setSubMchId(snapshot.getSubMchId());
        boolean hasSubMch = paySubMerchantOnboardingApi.hasEffectiveSubMchId(merchantTenantId.trim());
        vo.setHasEffectiveSubMchId(hasSubMch);
        if (!Boolean.TRUE.equals(snapshot.getNeedProfitSharing())) {
            vo.setCanShelf(true);
            vo.setMessage("当前为「" + SettleModeEnum.getDescByValue(snapshot.getSettleMode()) + "」，可直接上架");
            return vo;
        }
        if (hasSubMch) {
            vo.setCanShelf(true);
            String subMchId = StrUtil.blankToDefault(snapshot.getSubMchId(),
                    paySubMerchantOnboardingApi.resolveSubMchId(merchantTenantId.trim()));
            vo.setSubMchId(subMchId);
            vo.setMessage("微信分账商户已进件（sub_mch_id=" + StrUtil.blankToDefault(subMchId, "已生效") + "），可上架");
            return vo;
        }
        vo.setCanShelf(false);
        vo.setMessage("商户已启用微信分账结算，须先完成微信支付特约商户进件并获得 sub_mch_id 后才可上架");
        return vo;
    }
}
