#!/bin/bash

# Swagger 到 SpringDoc OpenAPI 注解迁移脚本

echo "开始 Swagger 到 SpringDoc OpenAPI 注解迁移..."

# 查找所有 Java 文件
find src/main/java -name "*.java" -type f | while read file; do
    echo "处理文件: $file"
    
    # 替换 import 语句
    sed -i '' 's/import io\.swagger\.annotations\.Api;/import io.swagger.v3.oas.annotations.tags.Tag;/g' "$file"
    sed -i '' 's/import io\.swagger\.annotations\.ApiModel;/import io.swagger.v3.oas.annotations.media.Schema;/g' "$file"
    sed -i '' 's/import io\.swagger\.annotations\.ApiModelProperty;/import io.swagger.v3.oas.annotations.media.Schema;/g' "$file"
    sed -i '' 's/import io\.swagger\.annotations\.ApiOperation;/import io.swagger.v3.oas.annotations.Operation;/g' "$file"
    sed -i '' 's/import io\.swagger\.annotations\.ApiParam;/import io.swagger.v3.oas.annotations.Parameter;/g' "$file"
    
    # 替换注解
    sed -i '' 's/@Api(tags = \([^)]*\))/@Tag(name = \1)/g' "$file"
    sed -i '' 's/@ApiModel(value = \([^,]*\), description = \([^)]*\))/@Schema(name = \1, description = \2)/g' "$file"
    sed -i '' 's/@ApiModel(description = \([^)]*\))/@Schema(description = \1)/g' "$file"
    sed -i '' 's/@ApiModelProperty(value = \([^)]*\))/@Schema(description = \1)/g' "$file"
    sed -i '' 's/@ApiModelProperty(\([^)]*\))/@Schema(description = \1)/g' "$file"
    sed -i '' 's/@ApiOperation(\([^)]*\))/@Operation(summary = \1)/g' "$file"
    sed -i '' 's/@ApiParam(\([^)]*\))/@Parameter(\1)/g' "$file"
    
done

echo "注解迁移完成！"

# 更新 SwaggerConfig.java
echo "更新 SwaggerConfig.java..."

cat > src/main/java/com/gxwebsoft/common/core/config/OpenApiConfig.java << 'EOF'
package com.gxwebsoft.common.core.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.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.Components;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * OpenAPI 配置
 *
 * @author WebSoft
 * @since 2025-09-11
 */
@Configuration
public class OpenApiConfig {
    
    @Resource
    private ConfigProperties config;

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title(config.getSwaggerTitle())
                        .description(config.getSwaggerDescription())
                        .version(config.getSwaggerVersion())
                        .contact(new Contact()
                                .name("科技小王子")
                                .url("https://websoft.top")
                                .email("170083662@qq.com")))
                .addSecurityItem(new SecurityRequirement().addList("Authorization"))
                .components(new Components()
                        .addSecuritySchemes("Authorization",
                                new SecurityScheme()
                                        .type(SecurityScheme.Type.HTTP)
                                        .scheme("bearer")
                                        .bearerFormat("JWT")
                                        .description("JWT 认证")));
    }
}
EOF

echo "OpenAPI 配置文件创建完成！"
echo "迁移脚本执行完成！请检查编译结果。"
