package com.aip.gateway.api.config;

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.models.GroupedOpenApi;
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 java.util.ArrayList;
import java.util.List;

@OpenAPIDefinition(
        info = @Info(title = "AIP Gateway API",
                description = "Azure Information Protection RESTFull API Service Gateway",
                version = "v1"))
@RequiredArgsConstructor
@Configuration
//@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public GroupedOpenApi chatOpenApi() {
        // "/v1/**" 경로에 매칭되는 API를 그룹화하여 문서화한다.
        String[] paths = {"/v1/**"};

        return GroupedOpenApi.builder()
                .group("aip-gateway-api")  // 그룹 이름을 설정 한다.
                .pathsToMatch(paths)     // 그룹에 속하는 경로 패턴을 지정 한다.
                .build();
    }
//    @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("aip-gateway-api")
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.aip.gateway.api.controller"))
                .paths(PathSelectors.any())
                //.paths(PathSelectors.ant("/v1/aip-api/**"))
                //.paths(regex("/api/vms/(control|esb)/.*"))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Azure Information Protection RESTFull API Service Gateway")
                .description("Azure Information Protection RESTFull API Service Gateway")
                .version("v1")
                .build();
    }

}
