package com.koobietech.eas.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import java.util.HashMap;

/**
 * @author lc
 */
@Configuration
@Profile("dev")
public class SwaggerConfiguration {

    @Bean
    public OpenAPI baseApi(@Value("${springdoc.version}") String appVersion){
        OpenAPI openApi = new OpenAPI();
        //基本信息
        Info info = new Info().title("爱扣钉教务接口")
            .description("爱扣钉教务接口API文档，openapi3.0 接口，用于前端对接")
            .version(appVersion)
            .contact(new Contact().name("王雁"))
            .license(new License().name("使用请遵守 Apache 2.0 授权协议")
            .url("http://edu.koobietech.com"));
        openApi.info(info);
        //授权信息
        SecurityScheme securityToken = new SecurityScheme()
                .type(SecurityScheme.Type.HTTP)
                .scheme("bearer")
                .bearerFormat("JWT")
                .in(SecurityScheme.In.HEADER)
                .name("Authorization");
        HashMap<String, SecurityScheme> authToken = new HashMap<>();
        authToken.put("Authorization", securityToken);
        openApi.components(new Components().securitySchemes(authToken));
        openApi.addSecurityItem(new SecurityRequirement().addList("Authorization"));
        return openApi;
    }

    @Bean
    public GroupedOpenApi baseRestApi() {
        return GroupedOpenApi.builder()
            .group("爱扣钉学员接口")
            .pathsToMatch("/**")
            .packagesToScan(new String[]{
                    "com.koobietech.eas.controller", "com.koobietech.eas.dao",
                    "com.koobietech.eas.mbg"
                }
            )
            .build();
    }
}
