package com.face.global.config;


import com.face.global.consts.GlobalConstant;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass(GroupedOpenApi.class)
@ConditionalOnProperty(value = "springdoc.swagger-ui.enabled",
        havingValue = "true", matchIfMissing = true)
public class SwaggerConfig {

    @Bean
    public GroupedOpenApi publicApi() {
        return GroupedOpenApi.builder()
                .group("public")
                .pathsToMatch("/api/customer/**")
                .build();
    }


    @Bean
    public GroupedOpenApi mobileManageApi() {
        return GroupedOpenApi.builder()
                .group("mobileAdmin")
                .pathsToMatch("/api/manage/**")
                .build();
    }

    @Bean
    public GroupedOpenApi adminApi() {
        return GroupedOpenApi.builder()
                .group("admin")
                .pathsToMatch("/face/**")
                .build();
    }

    @Bean
    public OpenAPI openApi() {
        return new OpenAPI()
                .addSecurityItem(new SecurityRequirement()
                        .addList(GlobalConstant.TOKEN_KEY))
                .components(new Components()
                        .addSecuritySchemes(GlobalConstant.TOKEN_KEY, new SecurityScheme()
                                .scheme("bearer")
                                .description("使用" + GlobalConstant.TOKEN_START_WITH_KEY + "+返回的token，" +
                                        "这里只使用返回的token，前端需要安置这种方式做，header的key为" + GlobalConstant.TOKEN_KEY)
                                .bearerFormat("JWT")
                                .type(SecurityScheme.Type.HTTP))
                        .addParameters(GlobalConstant.TOKEN_AUTH_TYPE, new Parameter()
                                .in("header").name(GlobalConstant.TOKEN_AUTH_TYPE)
                                .required(false).schema(new StringSchema())
                                .description("用户的授权类型header里面key为：" + GlobalConstant.TOKEN_AUTH_TYPE + "，值为登录的时候返回的授权类型"))
                        .addParameters(GlobalConstant.ORG_KEY, new Parameter()
                                .in("header").name(GlobalConstant.ORG_KEY)
                                .required(false).schema(new StringSchema())
                                .description("切换的机构信息header里面key为：" + GlobalConstant.ORG_KEY + "，用户选择的机构ID"))

                )
                // 文档描述信息
                .info(new Info()
                        .title("api")
                        .description("api document")
                        .version("1.0")
                );
    }
}
