package com.ruoyi.web.core.config;

import com.ruoyi.common.config.RuoYiConfig;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
import io.swagger.v3.oas.annotations.security.SecurityScheme;
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.security.SecurityRequirement;
import org.springdoc.core.GroupedOpenApi;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springdoc.core.providers.ObjectMapperProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * Swagger2的接口配置
 *
 * @author ruoyi
 */
@Configuration
@SecurityScheme(name = "Authorization", type = SecuritySchemeType.HTTP, scheme = "bearer", bearerFormat = "JWT")
public class SwaggerConfig {

    @Autowired
    private RuoYiConfig ruoyiConfig;

    @Bean
    public OpenAPI openAPI() {
        return new OpenAPI().info(new Info().title("若依管理系统_接口文档")
                .description("用于管理集团旗下公司的人员信息,具体包括XXX,XXX模块...")
                .contact(new Contact().name(ruoyiConfig.getName()))
                .version(ruoyiConfig.getVersion()));
    }

    @Bean
    PageModelsConverter pageModelsConverter(ObjectMapperProvider objectMapperProvider) {
        return new PageModelsConverter(objectMapperProvider);
    }

    @Bean
    public GlobalOpenApiCustomizer globalOpenApiCustomizer() {
        return openApi -> {
            if (openApi.getPaths() != null) {
                openApi.getPaths().forEach((s, pathItem) -> {
                    pathItem.readOperations().forEach(operation -> {
                        operation.addSecurityItem(new SecurityRequirement().addList("Authorization"));
                    });
                });
            }
        };
    }

    @Bean
    public GroupedOpenApi allApi() {
        String[] paths = {"/**"};
        return GroupedOpenApi.builder().group("all")
                .displayName("全部接口")
                .pathsToMatch(paths).build();
    }

}
