package com.haha.admin.config;

import com.qdb.sdk.QdbClient;
import com.qdb.sdk.QdbConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 企得宝 ERP SDK 配置类
 */
@Slf4j
@Configuration
public class QdbSdkConfig {

    @Value("${qdb.client-id:}")
    private String clientId;

    @Value("${qdb.client-secret:}")
    private String clientSecret;

    @Value("${qdb.api-url:https://api.7debao.com/router/api}")
    private String apiUrl;

    /**
     * 创建企得宝ERP SDK客户端Bean
     * 仅在配置了 client-id 和 client-secret 时才创建
     */
    @Bean
    @ConditionalOnProperty(prefix = "qdb", name = {"client-id", "client-secret"})
    public QdbClient qdbClient() {
        QdbConfig config = QdbConfig.builder()
                .clientId(clientId)
                .clientSecret(clientSecret)
                .apiUrl(apiUrl)
                .build();

        config.validate();

        log.info("企得宝ERP SDK 初始化成功, clientId: {}", clientId);
        return new QdbClient(config);
    }
}
