Param(
    [string]$ApiBaseUrl = "http://localhost:8000",
    [int]$Port = 5173
)

Set-Location -Path $PSScriptRoot

Write-Host "========================================"
Write-Host "启动 AI Swap 前端 (PowerShell)"
Write-Host "DIR: $PSScriptRoot"
Write-Host "VITE_API_BASE_URL: $ApiBaseUrl"
Write-Host "PORT: $Port"
Write-Host "========================================"

# 写入/更新 .env.local
Set-Content -Path ".env.local" -Value "VITE_API_BASE_URL=$ApiBaseUrl"

# 安装依赖并构建（如已构建可跳过）
# try {
#     npm ci | Out-Host
# } catch {
#     yarn install | Out-Host
# }

# npm run build | Out-Host

# 0.0.0.0:$Port 预览
npm run preview

# 运行本脚本命令
# ./frontend/run_frontend.ps1 -ApiBaseUrl http://ai-swap.local:8000 -Port 5173

