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"
)

$results = @()
$allFiles = Get-ChildItem -LiteralPath $Samples -Recurse -Filter "*.bas"
$total = $allFiles.Count

function Compile {
    param([string]$Path)
    $err = & $Compiler "`"$Path`"" -S 2>&1
    return @{ ExitCode = $LASTEXITCODE; Output = "$err" }
}

function Get-FirstRealError {
    param([string]$Output)
    if ([string]::IsNullOrEmpty($Output)) { return "" }
    $lines = $Output -split "`n"
    foreach ($line in $lines) {
        if ($line -match '^\S+\.bas:\d+:\d+:\s*error:') { return $line }
    }
    return $lines[0]
}

function Apply-Fixes {
    param([string]$Path, [string]$ErrorMsg)
    $content = Get-Content -LiteralPath $Path -Raw
    $changes = 0
    
    # Fix: STRING$(n) missing fill char
    if ($content -match 'STRING\$\((\d+)\)(?!\s*[,\)])') {
        $content = $content -replace 'STRING\$\((\d+)\)(?!\s*[,\)])', 'STRING$($1, "-")'
        $changes++
    }
    
    # Fix: WAITKEY$
    if ($content -match 'WAITKEY\$') {
        $content = $content -replace 'WAITKEY\$', 'SLEEP 3000'
        $changes++
    }
    
    # Fix: QUAD -> LONG
    if ($content -match '\bQUAD\b') { $content = $content -replace '\bQUAD\b', 'LONG'; $changes++ }
    
    # Fix: DWORD -> LONG
    if ($content -match '\bDWORD\b') { $content = $content -replace '\bDWORD\b', 'LONG'; $changes++ }
    
    # Fix: WORD -> LONG
    if ($content -match '\bWORD\b') { $content = $content -replace '\bWORD\b', 'LONG'; $changes++ }
    
    # Fix: BYTE -> LONG
    if ($content -match '\bBYTE\b') { $content = $content -replace '\bBYTE\b', 'LONG'; $changes++ }
    
    # Fix: SINGLE -> DOUBLE
    if ($content -match '\bSINGLE\b') { $content = $content -replace '\bSINGLE\b', 'DOUBLE'; $changes++ }
    
    # Fix: WSTRING references - remove 
    if ($content -match '\bWSTRING\b') {
        # Comment out lines with WSTRING
        $content = $content -replace '.*\bWSTRING\b.*', "' REMOVED WSTRING"
        $changes++
    }
    
    # Fix: FORMAT$ -> STR$
    if ($content -match 'FORMAT\$') {
        $content = $content -replace 'FORMAT\$\(([^)]+)\)', 'STR$($1)'
        $changes++
    }
    
    # Fix: DATE$ & " " & TIME$ -> simple string
    if ($content -match 'DATE\$\s*&') { $content = $content -replace 'DATE\$\s*&\s*"[^"]*"\s*&\s*TIME\$', '"Current date/time"'; $changes++ }
    
    # Fix: 2 ^ expr -> avoid power operator (replace with computed values or simpler)
    if ($content -match '\(2 \^ (\d+)\)') { 
        $content = $content -replace '\(2 \^ 1\)', '2'
        $content = $content -replace '\(2 \^ 2\)', '4'
        $content = $content -replace '\(2 \^ 3\)', '8'
        $content = $content -replace '\(2 \^ 4\)', '16'
        $content = $content -replace '\(2 \^ 5\)', '32'
        $content = $content -replace '\(2 \^ 6\)', '64'
        $content = $content -replace '\(2 \^ 7\)', '128'
        $content = $content -replace '\(2 \^ 8\)', '256'
        $content = $content -replace '\(2 \^ 9\)', '512'
        $content = $content -replace '\(2 \^ 10\)', '1024'
        $content = $content -replace '\(2 \^ 11\)', '2048'
        $content = $content -replace '\(2 \^ 12\)', '4096'
        $content = $content -replace '\(2 \^ 13\)', '8192'
        $content = $content -replace '\(2 \^ 14\)', '16384'
        $content = $content -replace '\(2 \^ 15\)', '32768'
        $content = $content -replace '\(2 \^ 16\)', '65536'
        $changes++
    }
    
    # Fix: Some common broken MID$ patterns
    if ($content -match 'MID\$\(MID\$\(MID\$\(MID\$\(') {
        # Replace nested MID$ with single MID$ calls - targeted fix for specific broken patterns
        $content = $content -replace 'ASC\(MID\$\(MID\$\(MID\$\(MID\$\(([^,]+),\s*(\d+),\s*(\d+),\s*\d+,\s*\d+,\s*\d+\)\)\)\)\)', 'ASC(MID$($1, $2, $3))'
        $content = $content -replace 'ASC\(MID\$\(MID\$\(MID\$\(MID\$\(([^,]+),\s*(\d+),\s*\d+,\s*\d+,\s*\d+,\s*\d+\)\)\)\)\)', 'ASC(MID$($1, $2, 1))'
        $changes++
    }
    
    if ($changes -gt 0) {
        Set-Content -LiteralPath $Path -Value $content -NoNewline
    }
    return $changes
}

# Process all files
foreach ($f in $allFiles) {
    $result = Compile -Path $f.FullName
    if ($result.ExitCode -eq 0) { continue }
    
    $errMsg = Get-FirstRealError -Output $result.Output
    $applied = Apply-Fixes -Path $f.FullName -ErrorMsg $errMsg
    
    if ($applied -gt 0) {
        $result2 = Compile -Path $f.FullName
        if ($result2.ExitCode -eq 0) { continue }
        $errMsg = Get-FirstRealError -Output $result2.Output
    }
    
    $results += [PSCustomObject]@{ Path = $f.FullName; Error = $errMsg }
}

# Summary
$passAfter = 0
$failAfter = 0
foreach ($f in $allFiles) {
    $r = Compile -Path $f.FullName
    if ($r.ExitCode -eq 0) { $passAfter++ } else { $failAfter++ }
}

Write-Output "=== FINAL AFTER AUTO-FIX ==="
Write-Output "Pass: $passAfter, Fail: $failAfter"
if ($results.Count -gt 0) {
    Write-Output "=== STILL FAILING ==="
    $results | ForEach-Object { Write-Output "$($_.Path): $($_.Error)" }
}
