param(
    [string]$Compiler = "F:\00_MR\MR_Komponents\Internet-SLL\Skeleton\PBX-Editor\TestUsage\PBXA64\pbxa64.exe",
    [string]$Samples = "F:\00_MR\MR_Komponents\Internet-SLL\Skeleton\PBX-Editor\TestUsage\PBXA64\Samples\PB"
)

# Known crash files - skip these
$crashFiles = @(
    "03_Operators\bitwise_flags.bas",
    "03_Operators\bit_reverse.bas",
    "04_Control_Flow\state_machine.bas",
    "06_Arrays\histogram.bas",
    "23_Inline_ASM\asm_cpuid.bas"
)

function Get-FirstError {
    param([string]$Path)
    $err = & $Compiler "`"$Path`"" -S 2>&1
    if ($LASTEXITCODE -eq 0) { return $null }
    $firstLine = ($err | Select-Object -Skip 1 | Select-Object -First 1)
    return "$firstLine"
}

function Fix-File {
    param([string]$Path)
    $content = Get-Content -LiteralPath $Path -Raw
    $changed = $false
    
    # Fix 1: STRING$(n) with 1 arg -> STRING$(n, " ")
    if ($content -match 'STRING\$\((\d+)\)(?!\s*[,\)])') {
        $content = $content -replace 'STRING\$\((\d+)\)(?!\s*[,\)])', 'STRING$($1, "-")'
        $changed = $true
    }
    
    # Fix 2: WAITKEY$ -> SLEEP 3000
    if ($content -match 'WAITKEY\$') {
        $content = $content -replace 'WAITKEY\$', 'SLEEP 3000'
        $changed = $true
    }
    
    # Fix 3: QUAD -> LONG
    if ($content -match '\bQUAD\b') {
        $content = $content -replace '\bQUAD\b', 'LONG'
        $changed = $true
    }
    
    # Fix 4: For variables without declaration
    # This is too complex for regex, skip
    
    # Fix 5: DATE$ & " " & TIME$ -> replace with simpler
    if ($content -match 'DATE\$\s*&\s*"[^"]*"\s*&\s*TIME\$') {
        $content = $content -replace 'DATE\$\s*&\s*"[^"]*"\s*&\s*TIME\$', '"date/time"'
        $changed = $true
    }
    
    # Fix 6: empty RETURN statements -> remove
    if ($content -match '^\s*RETURN\s*$') {
        $content = $content -replace '^\s*RETURN\s*$', "' RETURN removed"
        $changed = $true
    }
    
    if ($changed) {
        Set-Content -LiteralPath $Path -Value $content -NoNewline
    }
    return $changed
}

# Main loop - process all failing files
$allFiles = Get-ChildItem -LiteralPath $Samples -Recurse -Filter "*.bas"
$total = $allFiles.Count
$passCount = 0
$failCount = 0
$remainingFails = @()

foreach ($f in $allFiles) {
    $relPath = $f.FullName.Substring($Samples.Length + 1)
    
    # Check if crash file
    $isCrash = $false
    foreach ($cf in $crashFiles) {
        if ($relPath -eq $cf) { $isCrash = $true; break }
    }
    
    $err = & $Compiler "`"$($f.FullName)`"" -S 2>&1
    if ($LASTEXITCODE -eq 0) {
        $passCount++
        continue
    }
    
    # Try to fix
    $firstErr = ($err | Select-Object -Skip 1 | Select-Object -First 1)
    $fixed = Fix-File -Path $f.FullName
    
    if ($fixed) {
        # Recompile
        $err2 = & $Compiler "`"$($f.FullName)`"" -S 2>&1
        if ($LASTEXITCODE -eq 0) {
            $passCount++
            continue
        }
        $firstErr = ($err2 | Select-Object -Skip 1 | Select-Object -First 1)
    }
    
    $failCount++
    $remainingFails += [PSCustomObject]@{ Path = $relPath; Error = "$firstErr" }
}

Write-Output "=== FINAL SUMMARY ==="
Write-Output "Total: $total, Pass: $passCount, Fail: $failCount"
if ($remainingFails.Count -gt 0) {
    Write-Output "=== REMAINING FAILURES ==="
    $remainingFails | ForEach-Object { Write-Output "$($_.Path): $($_.Error)" }
}
