package com.ruoyi.crypto.config;

import java.security.Security;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
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;
import com.ruoyi.crypto.service.CryptoService;
import com.ruoyi.crypto.service.impl.StubCryptoServiceImpl;

/**
 * ruoyi-crypto 模块 Spring Boot 自动配置。
 * <p>
 * 通过 {@code META-INF/spring.factories} 注册；若容器中尚无 {@link CryptoService} Bean，
 * 则默认装配 {@link StubCryptoServiceImpl}。生产可另行提供 HSM 实现类并标注 {@code @Primary} 覆盖。
 * </p>
 */
@Configuration
@EnableConfigurationProperties(CryptoProperties.class)
public class CryptoAutoConfiguration
{
    static
    {
        if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null)
        {
            Security.addProvider(new BouncyCastleProvider());
        }
    }

    @Bean
    @ConditionalOnMissingBean(CryptoService.class)
    public CryptoService cryptoService(CryptoProperties properties)
    {
        return new StubCryptoServiceImpl(properties);
    }
}
