package com.qmth.boot.api.config;

import com.qmth.boot.core.fss.config.FssSecretProvider;
import com.qmth.boot.core.security.service.EncryptKeyProvider;
import com.qmth.boot.core.security.service.impl.DefaultEncryptKeyProvider;
import com.qmth.boot.core.solar.config.SolarProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.validation.constraints.NotNull;

@Configuration
@ComponentScan("com.qmth.boot.api")
@PropertySource("classpath:actuator.properties")
public class ApiAutoConfiguration {

    @Bean
    @ConditionalOnBean(SolarProperties.class)
    public EncryptKeyProvider encryptKeyProvider(@NotNull SolarProperties solarProperties) {
        return StringUtils.isNotBlank(solarProperties.getAppCode()) ?
                solarProperties::getAppCode :
                new DefaultEncryptKeyProvider();
    }

    @Bean
    public FssSecretProvider fssSecretProvider(EncryptKeyProvider encryptKeyProvider) {
        return encryptKeyProvider::getKey;
    }

}
