param(
    [string]$Prefix = "B-MULTI-"
)

$ErrorActionPreference = "Stop"

$WorkbookPath = Get-ChildItem -Path (Get-Location) -Filter "*.xlsx" |
    Where-Object { $_.Name -like "SaaS_E2E*" -and $_.Name -notlike "*副本*" } |
    Sort-Object LastWriteTime -Descending |
    Select-Object -First 1 -ExpandProperty FullName

$excel = $null
$workbook = $null

try {
    $excel = New-Object -ComObject Excel.Application
    $excel.Visible = $false
    $workbook = $excel.Workbooks.Open($WorkbookPath)
    $worksheet = $workbook.Worksheets.Item(1)
    $used = $worksheet.UsedRange

    for ($row = 2; $row -le $used.Rows.Count; $row++) {
        $caseId = ([string]$worksheet.Cells.Item($row, 1).Text).Trim()
        if ($caseId -like "$Prefix*") {
            Write-Host "---"
            Write-Host "$caseId | $($worksheet.Cells.Item($row, 5).Text)"
            Write-Host "Precondition=$($worksheet.Cells.Item($row, 7).Text)"
            Write-Host "Steps=$($worksheet.Cells.Item($row, 9).Text)"
            Write-Host "Expected=$($worksheet.Cells.Item($row, 10).Text)"
            Write-Host "Assert=$($worksheet.Cells.Item($row, 11).Text)"
        }
    }
}
finally {
    if ($workbook -ne $null) {
        $workbook.Close($false)
        [System.Runtime.InteropServices.Marshal]::ReleaseComObject($workbook) | Out-Null
    }
    if ($excel -ne $null) {
        $excel.Quit()
        [System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel) | Out-Null
    }
    [GC]::Collect()
    [GC]::WaitForPendingFinalizers()
}
