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

import cn.hutool.core.util.StrUtil;
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 com.jeesharp.business.modules.pay.paypufaconfig.entity.PayPufaConfig;
import com.jeesharp.business.modules.pay.paypufaconfig.enums.PayPufaConfigStatusEnum;
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 PayChannelPufaSyncSupport {

    private final PayChannelConfigService payChannelConfigService;

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