package org.dromara.common.doc.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.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.dromara.common.doc.config.properties.SpringDocProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 接口文档配置
 *
 * @author shihongwei
 */
@EnableConfigurationProperties(SpringDocProperties.class)
@ConditionalOnProperty(name = "springdoc.api-docs.enabled", havingValue = "true", matchIfMissing = true)
@Configuration
public class SpringDocConfig {

    /**
     * 创建OpenAPI配置
     */
    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(apiInfo())
                .addSecurityItem(new SecurityRequirement().addList("JWT Authentication").addList("ClientId"))
                .components(new Components()
                        .addSecuritySchemes("JWT Authentication", createAPIKeyScheme())
                        .addSecuritySchemes("ClientId", createClientIdScheme()));
    }

    /**
     * API信息配置
     */
    private Info apiInfo() {
        return new Info()
                .title("HOT平台API文档")
                .description("HOT平台后端接口文档，提供完整的API接口说明")
                .version("1.0.0")
                .contact(new Contact()
                        .name("xiaoshi98")
                        .email("178899525@qq.com")
                        .url("http://xiaoshi98.top"));
    }

    /**
     * 创建API Key安全方案
     */
    private SecurityScheme createAPIKeyScheme() {
        return new SecurityScheme()
                .type(SecurityScheme.Type.APIKEY)
                .in(SecurityScheme.In.HEADER)
                .name("Authorization")
                .description("请在此处输入JWT Token（无需Bearer前缀）");
    }

    private SecurityScheme createClientIdScheme() {
        return new SecurityScheme()
                .type(SecurityScheme.Type.APIKEY)
                .in(SecurityScheme.In.HEADER)
                .name("clientid");
    }

}
