package com.gxwebsoft.common.core.config;

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 io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.Components;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * SpringDoc OpenAPI 配置
 *
 * @author WebSoft
 * @since 2018-02-22 11:29:05
 */
@Configuration
public class SwaggerConfig {
    @Resource
    private ConfigProperties config;

    /**
     * 全局 OpenAPI 配置
     */
    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title(config.getSwaggerTitle())
                        .description(config.getSwaggerDescription())
                        .version(config.getSwaggerVersion())
                        .contact(new Contact()
                                .name("科技小王子")
                                .url("https://www.gxwebsoft.com")
                                .email("170083662@qq.com"))
                        .termsOfService(config.getServerUrl() + "/system"))
                .components(new Components()
                        .addSecuritySchemes("Authorization",
                                new SecurityScheme()
                                        .type(SecurityScheme.Type.HTTP)
                                        .scheme("bearer")
                                        .bearerFormat("JWT")
                                        .name("Authorization")
                                        .description("JWT Authorization header using the Bearer scheme")))
                .addSecurityItem(new SecurityRequirement().addList("Authorization"));
    }

    /**
     * Common 模块分组
     */
    @Bean
    public GroupedOpenApi commonApi() {
        return GroupedOpenApi.builder()
                .group("common")
                .pathsToMatch("/api/common/**", "/api/system/**", "/api/user/**", "/api/role/**", "/api/menu/**")
                .packagesToScan("com.gxwebsoft.common")
                .build();
    }

    /**
     * CMS 模块分组
     */
    @Bean
    public GroupedOpenApi cmsApi() {
        return GroupedOpenApi.builder()
                .group("cms")
                .pathsToMatch("/api/cms/**")
                .packagesToScan("com.gxwebsoft.cms")
                .build();
    }

    /**
     * Shop 模块分组
     */
    @Bean
    public GroupedOpenApi shopApi() {
        return GroupedOpenApi.builder()
                .group("shop")
                // 订单等用户侧接口在 shop 包内，但路径使用 /api/user/**（前端统一 user 侧 API 前缀）
                .pathsToMatch("/api/shop/**", "/api/user/**")
                .packagesToScan("com.gxwebsoft.shop")
                .build();
    }

    /**
     * OA 模块分组
     */
    @Bean
    public GroupedOpenApi oaApi() {
        return GroupedOpenApi.builder()
                .group("oa")
                .pathsToMatch("/api/oa/**")
                .packagesToScan("com.gxwebsoft.oa")
                .build();
    }

    /**
     * 其他模块分组
     */
    @Bean
    public GroupedOpenApi otherApi() {
        return GroupedOpenApi.builder()
                .group("other")
                .pathsToMatch("/api/docs/**", "/api/project/**", "/api/pwl/**", "/api/bszx/**", "/api/hjm/**")
                .packagesToScan("com.gxwebsoft.docs", "com.gxwebsoft.project", "com.gxwebsoft.pwl", "com.gxwebsoft.bszx", "com.gxwebsoft.hjm")
                .build();
    }
}
