package com.jeesharp.business.modules.pay.paychannelconfig.support;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.pay.payalipayconfig.entity.PayAlipayConfig;
import com.jeesharp.business.modules.pay.payalipayconfig.enums.PayAlipayConfigStatusEnum;
import com.jeesharp.business.modules.pay.paychannelconfig.PayChannelPlatformDefaultConfigCode;
import com.jeesharp.business.modules.pay.paychannelconfig.entity.PayChannelConfig;
import com.jeesharp.business.modules.pay.paychannelconfig.enums.PayChannelCodeEnum;
import com.jeesharp.business.modules.pay.paychannelconfig.enums.PayChannelConfigStatusEnum;
import com.jeesharp.business.modules.pay.paychannelconfig.service.PayChannelConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 保存支付宝子配置时同步维护 {@code pay_channel_config} 主表。
 * <p>新建/更新须分别走 {@link PayChannelConfigService#createPayChannelConfig} 与
 * {@link PayChannelConfigService#updatePayChannelConfig}，与微信同步逻辑一致。</p>
 *
 * @author jeesharp
 * @since 2026-05-25
 */
@Component
@RequiredArgsConstructor
public class PayChannelAlipaySyncSupport {

    private final PayChannelConfigService payChannelConfigService;

    public void syncFromAlipay(PayAlipayConfig alipay) {
        if (alipay == null) {
            return;
        }
        String configCode = StrUtil.blankToDefault(alipay.getConfigCode(), "DEFAULT");
        PayChannelConfig channel = null;
        if (StrUtil.isNotBlank(alipay.getChannelConfigId())) {
            channel = payChannelConfigService.get(alipay.getChannelConfigId());
        }
        if (channel == null) {
            channel = payChannelConfigService.findByChannelAndConfigCode(
                    PayChannelCodeEnum.ALIPAY.getCode(), configCode, alipay.getTenantId());
        }
        if (channel == null && "DEFAULT".equalsIgnoreCase(StrUtil.trim(configCode))) {
            channel = payChannelConfigService.findByChannelAndConfigCode(
                    PayChannelCodeEnum.ALIPAY.getCode(), PayChannelPlatformDefaultConfigCode.ALIPAY, alipay.getTenantId());
        }
        boolean isNew = channel == null;
        if (isNew) {
            channel = new PayChannelConfig();
            channel.setChannelCode(PayChannelCodeEnum.ALIPAY.getCode());
            channel.setConfigCode(configCode);
            channel.setDelFlag("0");
            channel.setSort(0);
        }
        channel.setConfigName(StrUtil.blankToDefault(alipay.getConfigName(), "支付宝"));
        Integer status = alipay.getStatus() != null ? alipay.getStatus() : PayChannelConfigStatusEnum.ENABLED.getValue();
        channel.setStatus(status);
        channel.setNotifyBaseUrl(alipay.getNotifyBaseUrl());
        channel.setPayCenterNotifyPath(StrUtil.blankToDefault(alipay.getPayCenterNotifyPath(),
                "/api/pay/payNotify/aliPayNotify"));
        channel.setTenantId(alipay.getTenantId());
        channel.setTenantCode(alipay.getTenantCode());
        if (isNew) {
            payChannelConfigService.createPayChannelConfig(channel);
        } else {
            payChannelConfigService.updatePayChannelConfig(channel);
        }
        alipay.setChannelConfigId(channel.getId());
        if (PayAlipayConfigStatusEnum.ENABLED.getValue().equals(alipay.getStatus())) {
            alipay.setStatus(PayChannelConfigStatusEnum.ENABLED.getValue());
        }
    }
}
