# IPTV Windows 构建脚本

Write-Host "🚀 开始构建 IPTV Windows 桌面应用..." -ForegroundColor Green
Write-Host ""

# 检查 Rust
try {
    $cargoVersion = cargo --version
    Write-Host "✓ Rust 已安装 ($cargoVersion)" -ForegroundColor Green
} catch {
    Write-Host "❌ Rust 未安装，请先安装:" -ForegroundColor Red
    Write-Host "   访问 https://rustup.rs/ 下载安装程序"
    Read-Host "按 Enter 退出"
    exit 1
}

Write-Host ""

# 步骤1: 构建 ui
Write-Host "📦 步骤 1/4: 构建 ui 应用..." -ForegroundColor Cyan
Set-Location ui
npm install
if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ npm install 失败" -ForegroundColor Red
    Read-Host "按 Enter 退出"
    exit 1
}

npm run build
if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ 构建 ui 失败" -ForegroundColor Red
    Read-Host "按 Enter 退出"
    exit 1
}

# 复制 API 数据
if (Test-Path "dist-web/api") {
    Remove-Item "dist-web/api" -Recurse -Force
}
Copy-Item -Path "public/api" -Destination "dist-web/api" -Recurse -Force
Set-Location ..

# 步骤2: 安装依赖
Write-Host "📦 步骤 2/4: 安装 Tauri 依赖..." -ForegroundColor Cyan
Set-Location desktop
npm install
if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ npm install 失败" -ForegroundColor Red
    Read-Host "按 Enter 退出"
    exit 1
}

# 步骤3: 构建
Write-Host "🔨 步骤 3/4: 构建 Windows 应用（这可能需要几分钟）..." -ForegroundColor Cyan
npx tauri build
if ($LASTEXITCODE -ne 0) {
    Write-Host "❌ 构建失败" -ForegroundColor Red
    Read-Host "按 Enter 退出"
    exit 1
}

# 步骤4: 检查输出
Write-Host "✅ 步骤 4/4: 检查输出..." -ForegroundColor Green
Write-Host ""

$msiPath = "src-tauri/target/release/bundle/msi"
$nsisPath = "src-tauri/target/release/bundle/nsis"

Write-Host "🎉 构建成功！" -ForegroundColor Green
Write-Host ""
Write-Host "📦 安装包位置:" -ForegroundColor Yellow

if (Test-Path $msiPath) {
    Get-ChildItem "$msiPath/*.msi" | ForEach-Object {
        Write-Host "   MSI: $($_.FullName)" -ForegroundColor Gray
    }
}

if (Test-Path $nsisPath) {
    Get-ChildItem "$nsisPath/*.exe" | ForEach-Object {
        Write-Host "   EXE: $($_.FullName)" -ForegroundColor Gray
    }
}

Write-Host ""
Write-Host "🚀 运行方式:" -ForegroundColor Green
Write-Host "   1. 安装 MSI 版本（推荐，有自动更新）"
Write-Host "   2. 或直接运行 EXE 版本（便携版）"
Write-Host ""
Read-Host "按 Enter 退出"
