﻿# 生成 swagger_ui_resources.cj：将 resources/openapi/ 下静态资源以 Base64 内联为 CJ 源常量。
# 用法：pwsh tools/gen-swagger-ui-resources.ps1
# 资源更新后重新运行此脚本即可。

$ErrorActionPreference = "Stop"
$root = Split-Path -Parent $PSScriptRoot
$inDir = Join-Path $root "resources\openapi"
$outFile = Join-Path $root "src\openapi\swagger_ui_resources.cj"

$files = @(
    @{ Name = "allHtml"; File = "all.html" },
    @{ Name = "singleHtml"; File = "single.html" },
    @{ Name = "swaggerUiBundleJs"; File = "swagger-ui-bundle.js" },
    @{ Name = "swaggerUiStandalonePresetJs"; File = "swagger-ui-standalone-preset.js" },
    @{ Name = "swaggerUiCss"; File = "swagger-ui.css" }
)

$sb = [System.Text.StringBuilder]::new()
[void]$sb.AppendLine("/*")
[void]$sb.AppendLine(" * 自动生成文件，请勿手动编辑。")
[void]$sb.AppendLine(" * 由 tools/gen-swagger-ui-resources.ps1 从 resources/openapi/ 生成。")
[void]$sb.AppendLine(" * Swagger UI 静态资源以 Base64 内联，运行时由 OpenApiUIMiddleware 解码输出。")
[void]$sb.AppendLine(" */")
[void]$sb.AppendLine("")
[void]$sb.AppendLine("package simcu::simapi.openapi")
[void]$sb.AppendLine("")
[void]$sb.AppendLine("/**")
[void]$sb.AppendLine(" * Swagger UI 内置静态资源（Base64 编码）。")
[void]$sb.AppendLine(" */")
[void]$sb.AppendLine("public class SwaggerUIResources {")

foreach ($f in $files) {
    $path = Join-Path $inDir $f.File
    if (-not (Test-Path $path)) {
        throw "资源文件不存在: $path"
    }
    $bytes = [System.IO.File]::ReadAllBytes($path)
    $b64 = [System.Convert]::ToBase64String($bytes)
    [void]$sb.AppendLine("    public static let $($f.Name): String = `"$b64`"")
    [void]$sb.AppendLine("")
}

[void]$sb.AppendLine("    private init() {}")
[void]$sb.AppendLine("}")

[System.IO.File]::WriteAllText($outFile, $sb.ToString(), [System.Text.UTF8Encoding]::new($false))
$size = (Get-Item $outFile).Length
Write-Host "已生成 $outFile ($size 字节)"