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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.pay.api.PaySubMerchantOnboardingApi;
import com.jeesharp.business.modules.pay.paychannelconfig.support.PayChannelTenantSupport;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.application.support.PaySubMerchantApplymentOperateSupport;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.entity.PaySubMerchantApplyment;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.enums.PaySubMerchantApplymentApiTypeEnum;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.service.PaySubMerchantApplymentService;
import com.jeesharp.business.modules.pay.paysubmerchantapplyment.vo.PaySubMerchantOnboardingSnapshotVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * {@link PaySubMerchantOnboardingApi} 实现，按商户租户查询进件单并判断 sub_mch_id 是否有效。
 *
 * @author jeesharp
 * @since 2026-06-11
 */
@Service
@AllArgsConstructor
public class PaySubMerchantOnboardingApiImpl implements PaySubMerchantOnboardingApi {

    private final PaySubMerchantApplymentService paySubMerchantApplymentService;

    @Override
    public boolean hasEffectiveSubMchId(String merchantTenantId) {
        PaySubMerchantOnboardingSnapshotVO snapshot = getSnapshot(merchantTenantId);
        return Boolean.TRUE.equals(snapshot.getEffective());
    }

    @Override
    public String resolveSubMchId(String merchantTenantId) {
        PaySubMerchantOnboardingSnapshotVO snapshot = getSnapshot(merchantTenantId);
        if (!Boolean.TRUE.equals(snapshot.getEffective())) {
            return null;
        }
        return snapshot.getSubMchId();
    }

    @Override
    public PaySubMerchantOnboardingSnapshotVO getSnapshot(String merchantTenantId) {
        PaySubMerchantOnboardingSnapshotVO vo = new PaySubMerchantOnboardingSnapshotVO();
        vo.setMerchantTenantId(merchantTenantId);
        vo.setHasApplyment(false);
        vo.setEffective(false);
        if (StrUtil.isBlank(merchantTenantId)) {
            vo.setMessage("商户租户不能为空");
            return vo;
        }
        String storageTenantId = PayChannelTenantSupport.toStorageKey(merchantTenantId.trim());
        PaySubMerchantApplyment applyment = paySubMerchantApplymentService.findByApplymentTenantId(storageTenantId);
        if (applyment == null) {
            vo.setMessage(PaySubMerchantApplymentOperateSupport.resolveIneffectiveMessage(null));
            return vo;
        }
        vo.setHasApplyment(true);
        vo.setSubMchId(StrUtil.trimToNull(applyment.getSubMchId()));
        vo.setApplyStatus(PaySubMerchantApplymentOperateSupport.normalizeApplyStatus(applyment.getApplyStatus()));
        vo.setApplymentApiType(PaySubMerchantApplymentApiTypeEnum.fromValue(applyment.getApplymentApiType()).getValue());
        vo.setEffective(PaySubMerchantApplymentOperateSupport.isEffectiveSubMerchant(applyment));
        vo.setMessage(PaySubMerchantApplymentOperateSupport.resolveIneffectiveMessage(applyment));
        return vo;
    }
}
