package com.its.api.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket commonApi() {
        Parameter parameterBuilder = new ParameterBuilder()
                .name(HttpHeaders.AUTHORIZATION)
                .description("Access Token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();

        List<Parameter> globalParameters = new ArrayList<>();
        globalParameters.add(parameterBuilder);
        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(globalParameters)
                .groupName("its-api")
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.its.api"))
                .paths(PathSelectors.any())
                //.paths(PathSelectors.ant("/api/**"))
                .build();

/*
Response를 보면 Swagger에서는 200, 401, 403, 404 같은 기본상태코드에 대한 디폴트메세지를 제공해준다.
    이 기본상태코드들을 제거하려면 Swagger JAVA 설정코드쪽에
    .securitySchemes(Collections.singletonList(apiKey())) 이부분을
    .securitySchemes(Collections.singletonList(apiKey())).useDefaultResponseMessages(false)
*/
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("ITS Operating API")
                .description("Intelligent Traffic Operating System API")
                .version("0.0.1")
                .build();
    }

}
