package com.haha.admin.config;

import com.haha.sdk.HahaClient;
import com.haha.sdk.config.HahaConfig;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 哈哈零售 SDK 配置类
 * 基于新版SDK重新配置
 */
@Configuration
public class HahaSdkConfig {

    @Value("${haha.api.base-url}")
    private String apiBaseUrl;

    @Value("${haha.api.app-id}")
    private String appId;

    @Value("${haha.api.app-secret}")
    private String appSecret;

    private HahaClient hahaClient;

    /**
     * 创建哈哈零售SDK客户端Bean
     * 注意：新版SDK包路径已更改为 com.haha.sdk.HahaClient
     */
    @Bean
    public HahaClient hahaClient() {
        // 创建SDK配置
        HahaConfig config = HahaConfig.builder()
                .apiBaseUrl(apiBaseUrl)  // 新SDK使用apiBaseUrl
                .appId(appId)
                .appSecret(appSecret)
                .connectTimeout(10)
                .readTimeout(30)
                .writeTimeout(30)
                .enableLog(true)
                .build();

        // 校验配置
        config.validate();

        // 创建客户端
        this.hahaClient = new HahaClient(config);
        return this.hahaClient;
    }

    @PreDestroy
    public void destroy() {
        if (hahaClient != null) {
            hahaClient.shutdown();
        }
    }
}
