package cn.hztech.sdk.config;

import cn.hztech.sdk.api.*;
import cn.hztech.sdk.client.SdkHttpClient;
import cn.hztech.sdk.crypto.envelope.GmPacketBuilder;
import cn.hztech.sdk.crypto.envelope.GmPacketParser;
import cn.hztech.sdk.crypto.sm2.SM2Util;
import cn.hztech.sdk.crypto.sm4.SM4Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * SDK自动配置类
 *
 * 功能：
 * - 自动配置所有SDK组件
 * - 支持Spring Boot自动装配
 * - 提供可覆盖的Bean配置
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(SdkProperties.class)
public class SdkAutoConfiguration {

    private final SdkProperties properties;

    public SdkAutoConfiguration(SdkProperties properties) {
        this.properties = properties;
        log.info("初始化一单位一平台SDK，应用ID: {}", properties.getAppId());
    }

    /**
     * 配置SM2工具
     */
    @Bean
    @ConditionalOnMissingBean
    public SM2Util sm2Util() {
        return new SM2Util();
    }

    /**
     * 配置SM4工具
     */
    @Bean
    @ConditionalOnMissingBean
    public SM4Util sm4Util() {
        return new SM4Util();
    }

    /**
     * 配置报文构建器
     */
    @Bean
    @ConditionalOnMissingBean
    public GmPacketBuilder gmPacketBuilder(SM2Util sm2Util, SM4Util sm4Util) {
        return new GmPacketBuilder(
            properties.getAppId(),
            properties.getPrivateKey(),
            properties.getPlatformPublicKey(),
            sm2Util,
            sm4Util
        );
    }

    /**
     * 配置报文解析器
     */
    @Bean
    @ConditionalOnMissingBean
    public GmPacketParser gmPacketParser(SM2Util sm2Util, SM4Util sm4Util) {
        return new GmPacketParser(
            properties.getPrivateKey(),
            properties.getPlatformPublicKey(),
            sm2Util,
            sm4Util,
            properties.getTimestampTolerance()
        );
    }

    /**
     * 配置HTTP客户端
     */
    @Bean
    @ConditionalOnMissingBean
    public SdkHttpClient sdkHttpClient(GmPacketBuilder packetBuilder,
                                       GmPacketParser packetParser) {
        return new SdkHttpClient(
            properties.getGatewayUrl(),
            properties.getConnectTimeout(),
            properties.getReadTimeout(),
            properties.getWriteTimeout(),
            packetBuilder,
            packetParser,
            properties.getEnableLog(),
            properties.getEnableRetry(),
            properties.getMaxRetries(),
            properties.getRetryInterval()
        );
    }

    /**
     * 配置UserApiClient
     */
    @Bean
    @ConditionalOnMissingBean
    public UserApiClient userApiClient(SdkHttpClient httpClient) {
        return new UserApiClient(httpClient);
    }

    /**
     * 配置DepartmentApiClient
     */
    @Bean
    @ConditionalOnMissingBean
    public DepartmentApiClient departmentApiClient(SdkHttpClient httpClient) {
        return new DepartmentApiClient(httpClient);
    }

    /**
     * 配置RoleApiClient
     */
    @Bean
    @ConditionalOnMissingBean
    public RoleApiClient roleApiClient(SdkHttpClient httpClient) {
        return new RoleApiClient(httpClient);
    }

    /**
     * 配置MenuApiClient
     */
    @Bean
    @ConditionalOnMissingBean
    public MenuApiClient menuApiClient(SdkHttpClient httpClient) {
        return new MenuApiClient(httpClient);
    }

    /**
     * 配置PermissionApiClient
     */
    @Bean
    @ConditionalOnMissingBean
    public PermissionApiClient permissionApiClient(SdkHttpClient httpClient) {
        return new PermissionApiClient(httpClient);
    }

    /**
     * 配置ApiManagementApiClient
     */
    @Bean
    @ConditionalOnMissingBean
    public ApiManagementApiClient apiManagementApiClient(SdkHttpClient httpClient) {
        return new ApiManagementApiClient(httpClient);
    }

    /**
     * 配置MessageApiClient
     */
    @Bean
    @ConditionalOnMissingBean
    public MessageApiClient messageApiClient(SdkHttpClient httpClient) {
        return new MessageApiClient(httpClient);
    }

    /**
     * 配置GatewayApiClient
     */
    @Bean
    @ConditionalOnMissingBean
    public GateWayApiClient gateWayApiClient(SdkHttpClient httpClient) {
        return new GateWayApiClient(httpClient);
    }
}
