package dp.kotul.starter.autoconfig

import dp.kotul.starter.properties.SwaggerProperties
import io.swagger.annotations.Api
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 springfox.documentation.builders.ApiInfoBuilder
import springfox.documentation.builders.PathSelectors
import springfox.documentation.builders.RequestHandlerSelectors
import springfox.documentation.service.ApiInfo
import springfox.documentation.spi.DocumentationType
import springfox.documentation.spring.web.plugins.Docket

@Configuration
@EnableConfigurationProperties(SwaggerProperties::class)
class SwaggerAutoConfiguration(val swaggerProperties: SwaggerProperties) {
    @Bean
    @ConditionalOnMissingBean
    fun swagger(): Docket {
        return Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api::class.java))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(swaggerInfo())
    }

    private fun swaggerInfo(): ApiInfo {
        return ApiInfoBuilder()
                .title(swaggerProperties.apiTitle)
                .description(swaggerProperties.apiDescription)
                .version(swaggerProperties.apiVersion)
                .build()
    }
}
