package com.kingdee.eas.custom.integration.apimgt.config;

import com.kingdee.eas.custom.integration.apimgt.auth.interceptor.OpenApiAuthInterceptor;
import com.kingdee.eas.custom.integration.apimgt.auth.service.BosAuthDao;
import com.kingdee.eas.custom.integration.apimgt.auth.service.SecretAuthService;
import com.kingdee.eas.custom.integration.base.config.OpenApiConfig;
import com.kingdee.eas.custom.integration.base.service.IntegrationConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AuthConfig {

    private static final Logger logger = LoggerFactory.getLogger(AuthConfig.class);

    @Bean
    public BosAuthDao bosAuthDao() {
        return new BosAuthDao();
    }

    @Bean
    public SecretAuthService secretAuthService(BosAuthDao bosAuthDao) {
        return new SecretAuthService(bosAuthDao);
    }

    @Bean
    public OpenApiAuthInterceptor openApiAuthInterceptor(
            SecretAuthService secretAuthService,
            IntegrationConfigService appConfigService) {
        logger.info("Configuring OpenApiAuthInterceptor");
        OpenApiAuthInterceptor interceptor = new OpenApiAuthInterceptor(secretAuthService);
        // 获取OpenApi配置
        OpenApiConfig openApiConfig = appConfigService.getOpenApiConfig();
        // 设置是否启用API认证
        interceptor.setAuthEnabled(openApiConfig.isEnableAuth());

        logger.info("OpenApiAuthInterceptor configured: authEnabled={}", openApiConfig.isEnableAuth());
        return interceptor;
    }
}
