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.*;
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.Arrays;
import java.util.HashMap;

@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");
        openAPI.components(new Components().securitySchemes(new HashMap<>(){{
            put("Authorization", securityToken);
        }}));
        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();
    }
}
