package dp.kotul.starter.config

import dp.kotul.starter.properties.FeignProperties
import feign.Logger
import feign.Request
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 java.util.concurrent.TimeUnit

@Configuration
@EnableConfigurationProperties(FeignProperties::class)
class FeignConfiguration(
    val feignProperties: FeignProperties
) {

    @Bean
    @ConditionalOnMissingBean
    fun loggerLevel(): Logger.Level {
        return Logger.Level.valueOf(feignProperties.level)
    }

    @Bean
    @ConditionalOnMissingBean
    fun requestOptions(): Request.Options {
        return Request.Options(
            feignProperties.timeOut, TimeUnit.MILLISECONDS, feignProperties.timeOut, TimeUnit.MILLISECONDS, false
        )
    }
}
