name: 'Cleanup Processes (Windows)'
description: 'Kill all lemonade, Python, llama-server, and FLM processes on Windows'
runs:
  using: "composite"
  steps:
    - name: Kill zombie processes
      shell: PowerShell
      run: |
        Write-Host "========================================" -ForegroundColor Cyan
        Write-Host "Cleaning up processes on Windows..." -ForegroundColor Cyan
        Write-Host "========================================" -ForegroundColor Cyan

        # Function to safely kill processes by name pattern
        function Stop-ProcessByPattern {
            param(
                [string]$Pattern,
                [string]$Description
            )

            Write-Host "`nSearching for $Description processes..." -ForegroundColor Yellow
            $processes = Get-Process | Where-Object { $_.ProcessName -like "*$Pattern*" }

            if ($processes) {
                Write-Host "Found $($processes.Count) $Description process(es):" -ForegroundColor Yellow
                $processes | ForEach-Object {
                    Write-Host "  - $($_.ProcessName) (PID: $($_.Id))" -ForegroundColor Gray
                }

                $processes | ForEach-Object {
                    try {
                        Stop-Process -Id $_.Id -Force -ErrorAction SilentlyContinue
                        Write-Host "  Killed: $($_.ProcessName) (PID: $($_.Id))" -ForegroundColor Green
                    } catch {
                        Write-Host "  Failed to kill: $($_.ProcessName) (PID: $($_.Id)) - $_" -ForegroundColor Red
                    }
                }
            } else {
                Write-Host "No $Description processes found." -ForegroundColor Gray
            }
        }

        # Kill llama-server processes
        Stop-ProcessByPattern -Pattern "llama-server" -Description "llama-server"
        Stop-ProcessByPattern -Pattern "llama" -Description "llama"

        # Kill FLM processes (Windows only)
        Stop-ProcessByPattern -Pattern "flm" -Description "FLM"

        # Kill lemonade processes (lemond, lemonade, lemonade-tray, etc.)
        Stop-ProcessByPattern -Pattern "lemonade" -Description "lemonade"
        Stop-ProcessByPattern -Pattern "lemond" -Description "lemond"

        # Kill Python processes
        Stop-ProcessByPattern -Pattern "python" -Description "Python"

        # Kill wscript processes (VBScript launcher used by lemonade)
        Stop-ProcessByPattern -Pattern "wscript" -Description "wscript"

        # Wait a moment for processes to fully terminate
        Start-Sleep -Seconds 2

        # Attempt to uninstall legacy C++ NSIS-based Lemonade Server, if present
        Write-Host "`nChecking for legacy Lemonade Server NSIS installer..." -ForegroundColor Yellow
        $nsisKey = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\Lemonade Server'
        if (Test-Path $nsisKey) {
            try {
                $nsisProps = Get-ItemProperty -Path $nsisKey -ErrorAction Stop
                $uninstallExe = $nsisProps.UninstallString
                if ($uninstallExe) {
                    Write-Host "  Found NSIS uninstall registry entry. UninstallString: $uninstallExe" -ForegroundColor Gray
                    if (Test-Path $uninstallExe) {
                        Write-Host "  Running NSIS uninstaller silently..." -ForegroundColor Yellow
                        try {
                            & $uninstallExe /S | Out-Null
                            Write-Host "  NSIS uninstaller process invoked." -ForegroundColor Green
                        } catch {
                            Write-Host "  NSIS uninstaller execution failed: $_" -ForegroundColor Red
                        }
                    } else {
                        Write-Host "  NSIS uninstall executable path is stale (file not found). Treating as already removed." -ForegroundColor Gray
                    }
                } else {
                    Write-Host "  NSIS uninstall string missing in registry. Treating as stale entry." -ForegroundColor Gray
                }

                # After running (or skipping) the uninstaller, ensure the ARP key is gone so MSI is not blocked
                if (Test-Path $nsisKey) {
                    Write-Host "  Removing stale NSIS ARP registry key to unblock MSI install..." -ForegroundColor Yellow
                    try {
                        Remove-Item -Path $nsisKey -Recurse -Force -ErrorAction Stop
                        Write-Host "  NSIS ARP registry key removed." -ForegroundColor Green
                    } catch {
                        Write-Host "  Failed to remove NSIS ARP registry key: $_" -ForegroundColor Red
                    }
                } else {
                    Write-Host "  NSIS ARP registry key already removed by uninstaller." -ForegroundColor Green
                }
            } catch {
                Write-Host "  Failed to query NSIS uninstall key: $_" -ForegroundColor Red
            }
        } else {
            Write-Host "  No legacy C++ NSIS installer registry key detected in HKCU (already clean)" -ForegroundColor Gray
        }

        # Clean up Python user directory that can cause issues
        Write-Host "`nCleaning up system Python directory..." -ForegroundColor Yellow
        $systemPythonPath = "C:\windows\system32\config\systemprofile\AppData\Roaming\Python"
        if (Test-Path $systemPythonPath) {
            try {
                Remove-Item -Path $systemPythonPath -Recurse -Force -ErrorAction SilentlyContinue
                Write-Host "  Cleaned: $systemPythonPath" -ForegroundColor Green
            } catch {
                Write-Host "  Failed to clean: $systemPythonPath - $_" -ForegroundColor Red
            }
        } else {
            Write-Host "  System Python directory not found (already clean)" -ForegroundColor Gray
        }

        # Clean up log files to avoid confusion between test runs
        Write-Host "`nCleaning up log files..." -ForegroundColor Yellow
        $logPatterns = @("lemonade*.log", "lemond*.log", "lemonade-server*.log")
        foreach ($pattern in $logPatterns) {
            $logFiles = Get-ChildItem "$env:TEMP\$pattern" -ErrorAction SilentlyContinue
            if ($logFiles) {
                Write-Host "  Found $($logFiles.Count) log file(s) matching '$pattern'" -ForegroundColor Gray
                $logFiles | ForEach-Object {
                    try {
                        Remove-Item $_.FullName -Force -ErrorAction SilentlyContinue
                        Write-Host "    Removed: $($_.Name)" -ForegroundColor Green
                    } catch {
                        Write-Host "    Failed to remove: $($_.Name)" -ForegroundColor Red
                    }
                }
            }
        }

        Write-Host "`n========================================" -ForegroundColor Cyan
        Write-Host "Process cleanup complete!" -ForegroundColor Cyan
        Write-Host "========================================" -ForegroundColor Cyan

    - name: Uninstall Lemonade Server (MSI)
      shell: PowerShell
      run: |
        Write-Host "`n========================================" -ForegroundColor Magenta
        Write-Host "Uninstalling Lemonade Server (MSI)..." -ForegroundColor Magenta
        Write-Host "========================================" -ForegroundColor Magenta

        # Uninstall by Product Code - Attempt to find it first
        $productName = "Lemonade Server"

        Write-Host "Searching for product: $productName" -ForegroundColor Cyan

        $product = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -eq $productName }

        if ($product) {
            Write-Host "Found installed product: $($product.Name) (Version: $($product.Version))" -ForegroundColor Green
            Write-Host "IdentifyingNumber (ProductCode): $($product.IdentifyingNumber)" -ForegroundColor Gray

            Write-Host "Uninstalling..." -ForegroundColor Yellow
            $proc = Start-Process -FilePath "msiexec.exe" -ArgumentList "/x $($product.IdentifyingNumber) /qn" -Wait -PassThru

            if ($proc.ExitCode -eq 0) {
                Write-Host "Uninstall successful!" -ForegroundColor Green
            } else {
                Write-Host "Uninstall failed with exit code $($proc.ExitCode)" -ForegroundColor Red
            }
        } else {
            Write-Host "Product '$productName' not found installed via MSI." -ForegroundColor Gray

            # Fallback: Check if we can uninstall via the MSI file if it exists in current directory
            if (Test-Path "lemonade-server-minimal.msi") {
                Write-Host "Found lemonade-server-minimal.msi in current directory. Attempting uninstall via file..." -ForegroundColor Yellow
                $proc = Start-Process -FilePath "msiexec.exe" -ArgumentList "/x lemonade-server-minimal.msi /qn" -Wait -PassThru

                if ($proc.ExitCode -eq 0) {
                    Write-Host "Uninstall via MSI file successful!" -ForegroundColor Green
                } else {
                    Write-Host "Uninstall via MSI file failed with exit code $($proc.ExitCode)" -ForegroundColor Red
                }
            }
        }
