package com.sckw.auth.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.info.License;
import io.swagger.v3.oas.models.servers.Server;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author PC
 */
@Configuration
public class OpenApiConfig implements WebMvcConfigurer {

    @Value("${spring.application.name:Application}")
    private String applicationName;

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title(applicationName + " API")
                        .version("1.0.0")
                        .description("物流登录DOC文档 - 提供登录管理以及相关API接口")
                        .contact(new Contact()
                                .name("开物登录开发团队")
                                .email("iot-dev@example.com")
                                .url("https://iot-platform.com"))
                        .license(new License()
                                .name("Apache 2.0")
                                .url("https://www.apache.org/licenses/LICENSE-2.0")))
                .servers(List.of(
                        new Server()
                                .url("http://localhost:10160")
                                .description("开发环境"),
                        new Server()
                                .url("http://10.10.10.224:10160/")
                                .description("测试环境"),
                        new Server()
                                .url("https://api.auth.example.com")
                                .description("生产环境")
                ));
    }

    @Bean
    public GroupedOpenApi authApi() {
        return GroupedOpenApi.builder()
                .group("auth")
                .packagesToScan("com.sckw.auth.controller")
                .build();
    }

    @Bean
    public GroupedOpenApi authAppApi() {
        return GroupedOpenApi.builder()
                .group("auth-app")
                .packagesToScan("com.sckw.auth.controller.app")
                .build();
    }

    @Bean
    public GroupedOpenApi authOperateApi() {
        return GroupedOpenApi.builder()
                .group("auth-operate")
                .packagesToScan("com.sckw.auth.controller.operate")
                .build();
    }

    @Bean
    public GroupedOpenApi authAllApi() {
        return GroupedOpenApi.builder()
                .group("auth-all")
                .packagesToScan("com.sckw.auth.controller",
                        "com.sckw.auth.controller.app",
                        "com.sckw.auth.controller.operate")
                .build();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}