package com.erp.common.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.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 统一 OpenAPI 3.0 配置
 *
 * 使用方式：在业务服务的 application.yml 中配置：
 *   erp:
 *     openapi:
 *       title: xxx-service API
 *       description: xxx服务API文档
 *       service-url: https://api.erpzbbh.cn
 *
 * 访问 Swagger UI: /swagger-ui.html
 * 访问 Knife4j: /doc.html
 */
@Configuration
public class OpenApiConfig {

    @Value("${erp.openapi.title:ERP Service API}")
    private String title;

    @Value("${erp.openapi.description:ERP Service API Documentation}")
    private String description;

    @Value("${erp.openapi.service-url:https://api.erpzbbh.cn}")
    private String serviceUrl;

    @Value("${erp.openapi.version:1.0.0}")
    private String version;

    @Bean
    public OpenAPI erpOpenAPI() {
        Server devServer = new Server();
        devServer.setUrl("http://localhost:{port}");
        devServer.setDescription("Development Server");

        Server prodServer = new Server();
        prodServer.setUrl(serviceUrl);
        prodServer.setDescription("Production Server");

        Contact contact = new Contact();
        contact.setName("ERP Team");
        contact.setEmail("dev@erpzbbh.cn");

        License license = new License()
                .name("Private License")
                .url("https://erpzbbh.cn/license");

        Info info = new Info()
                .title(title)
                .version(version)
                .description(description)
                .contact(contact)
                .license(license);

        return new OpenAPI()
                .info(info)
                .servers(List.of(devServer, prodServer));
    }
}
