package com.sckw.fleet.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:10080")
                                .description("开发环境"),
                        new Server()
                                .url("http://10.10.10.224:10080")
                                .description("测试环境"),
                        new Server()
                                .url("https://api.fleet.example.com")
                                .description("生产环境")
                ));
    }

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

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

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

    @Bean
    public GroupedOpenApi fleetAllApi() {
        return GroupedOpenApi.builder()
                .group("fleet-all")
                .packagesToScan("com.sckw.fleet.controller",
                        "com.sckw.fleet.controller.app",
                        "com.sckw.fleet.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/");
    }
}