# =============================================================================
# FWAuto Flash 測試腳本 (PowerShell)
# =============================================================================

# 測試專案路徑
$ProjectDir = "C:\STM32F407\examples\實例4 串口通訊實例"
$FwautoDir = "$ProjectDir\.fwauto"
$FwautoSource = "C:\Users\kentc\fwauto"

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "FWAuto Flash 測試腳本" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# -----------------------------------------------------------------------------
# Step 1: 安裝最新的 fwauto
# -----------------------------------------------------------------------------
Write-Host "[Step 1/6] 安裝最新的 fwauto..." -ForegroundColor Cyan

Set-Location $FwautoSource
uv pip install -e . *> $null

if ($LASTEXITCODE -eq 0) {
    Write-Host "✓ fwauto 安裝成功" -ForegroundColor Green
} else {
    Write-Host "✗ fwauto 安裝失敗" -ForegroundColor Red
    exit 1
}
Write-Host ""

# -----------------------------------------------------------------------------
# Step 2: 刪除 .fwauto 目錄
# -----------------------------------------------------------------------------
Write-Host "[Step 2/6] 清理舊的 .fwauto 目錄..." -ForegroundColor Cyan

if (Test-Path $FwautoDir) {
    Remove-Item -Path $FwautoDir -Recurse -Force
    Write-Host "✓ 已刪除 $FwautoDir" -ForegroundColor Green
} else {
    Write-Host "⚠ .fwauto 目錄不存在，跳過" -ForegroundColor Yellow
}
Write-Host ""

# -----------------------------------------------------------------------------
# Step 3: 執行 fwauto init
# -----------------------------------------------------------------------------
Write-Host "[Step 3/6] 執行 fwauto init..." -ForegroundColor Cyan

Set-Location $ProjectDir
"" | fwauto init

if (Test-Path $FwautoDir) {
    Write-Host "✓ fwauto init 完成" -ForegroundColor Green
} else {
    Write-Host "✗ fwauto init 失敗" -ForegroundColor Red
    exit 1
}
Write-Host ""

# -----------------------------------------------------------------------------
# Step 5: 執行 fwauto flash
# -----------------------------------------------------------------------------
Write-Host "[Step 5/6] 執行 fwauto flash..." -ForegroundColor Cyan

Set-Location $ProjectDir
$FlashOutput = fwauto flash 2>&1 | Out-String
$FlashExitCode = $LASTEXITCODE

Write-Host $FlashOutput
Write-Host ""

# -----------------------------------------------------------------------------
# Step 6: 驗證執行結果
# -----------------------------------------------------------------------------
Write-Host "[Step 6/6] 驗證執行結果..." -ForegroundColor Cyan

if ($FlashExitCode -eq 0) {
    Write-Host "✓ fwauto flash 執行成功 (exit code: 0)" -ForegroundColor Green
} else {
    Write-Host "✗ fwauto flash 執行失敗 (exit code: $FlashExitCode)" -ForegroundColor Red
}

if ($FlashOutput -match "Flash succeeded|Flash complete") {
    Write-Host "✓ Flash 成功訊息已確認" -ForegroundColor Green
} else {
    Write-Host "⚠ 未找到 Flash 成功訊息" -ForegroundColor Yellow
}

$LatestLog = Get-ChildItem "$FwautoDir\logs\fwauto_*.log" -ErrorAction SilentlyContinue |
             Sort-Object LastWriteTime -Descending |
             Select-Object -First 1

if ($LatestLog) {
    Write-Host "✓ Log 檔案: $($LatestLog.Name)" -ForegroundColor Green

    $LogContent = Get-Content $LatestLog.FullName -Raw

    if ($LogContent -match "flash_status.*success") {
        Write-Host "✓ Log 顯示 flash_status: success" -ForegroundColor Green
    }

    if ($LogContent -match "Exit code: 0") {
        Write-Host "✓ Log 顯示 Exit code: 0" -ForegroundColor Green
    }
} else {
    Write-Host "⚠ 未找到 log 檔案" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
if ($FlashExitCode -eq 0) {
    Write-Host "✅ 測試完成！" -ForegroundColor Green
} else {
    Write-Host "❌ 測試失敗！" -ForegroundColor Red
}
Write-Host "========================================" -ForegroundColor Cyan

exit $FlashExitCode
