package com.schoolinout.web.core.config;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.PathItem;
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.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;

/**
 * @author sakura
 * @date 2024/1/2 16:16:17 Tue
 */
@Configuration
public class SwaggerKnife4jConfig {

    @Value("${token.header:Authorization}")
    private String tokenName;

    /**
     * 根据@Tag 上的排序，写入x-order
     *
     * @return the global open api customizer
     */
    @Bean
    public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
        return openApi -> {
            // 全局添加鉴权参数
            if (openApi.getPaths() != null) {
                openApi.getPaths().forEach((s, pathItem) -> setSecurityHead(pathItem));
            }
        };
    }

    /**
     * 设置请求头
     *
     * @param pathItem 请求路径参数
     */
    private void setSecurityHead(PathItem pathItem) {
        pathItem.readOperations().forEach(operation -> operation.addSecurityItem(new SecurityRequirement().addList(tokenName)));
    }

    @Bean(value = "systemApi")
    public GroupedOpenApi systemApi() {
        return GroupedOpenApi.builder()
                .group("系统接口文档")
                .packagesToScan("com.schoolinout.web.controller.system")
                .addOpenApiCustomiser(orderGlobalOpenApiCustomizer())
                // 设置读取那些标识的接口
                .addOpenApiMethodFilter(method -> method.isAnnotationPresent(Operation.class))
                .build();
    }

    @Bean(value = "customerApi")
    public GroupedOpenApi customerApi() {
        return GroupedOpenApi.builder()
                .group("客户端接口文档")
                .packagesToScan("com.schoolinout.web.controller.api")
                .addOpenApiCustomiser(orderGlobalOpenApiCustomizer())
                // 设置读取那些标识的接口
                .addOpenApiMethodFilter(method -> method.isAnnotationPresent(Operation.class))
                .build();
    }


    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title("学生进出校管理平台试点版")
                        .version("1.0")
                        .description("学生进出校管理平台试点版 本次项目先不用对接闸机，人脸识别通过后，保安帮开门")
                        .termsOfService("#")
                        .license(new License().name("HJY")
                                .url("#"))
                ).addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION))
                .components(new Components().addSecuritySchemes(HttpHeaders.AUTHORIZATION, new SecurityScheme()
                        .name(HttpHeaders.AUTHORIZATION).type(SecurityScheme.Type.HTTP).scheme("bearer")));
    }


}
