package com.sckw.payment.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;

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

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

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

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

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