# PowerShell script to restart WSL
# Run from Windows PowerShell: .\scripts\restart-wsl.ps1

Write-Host "==========================================" -ForegroundColor Cyan
Write-Host "WSL Restart Script" -ForegroundColor Cyan
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host ""

# Check if running as administrator
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

if (-not $isAdmin) {
    Write-Host "Note: Not running as administrator. Some operations may require elevation." -ForegroundColor Yellow
    Write-Host ""
}

# List current WSL distributions
Write-Host "Current WSL distributions:" -ForegroundColor Green
wsl --list --verbose
Write-Host ""

# Ask for confirmation
$response = Read-Host "Do you want to restart WSL? (y/N)"
if ($response -ne 'y' -and $response -ne 'Y') {
    Write-Host "Cancelled." -ForegroundColor Yellow
    exit
}

Write-Host ""
Write-Host "Restarting WSL (Ubuntu)..." -ForegroundColor Green
wsl --terminate Ubuntu

if ($LASTEXITCODE -eq 0) {
    Write-Host "✅ WSL (Ubuntu) terminated successfully." -ForegroundColor Green
    Write-Host ""
    Write-Host "WSL will restart automatically when you connect to it again." -ForegroundColor Cyan
    Write-Host "Reconnect to WSL in Cursor to verify the fix." -ForegroundColor Cyan
} else {
    Write-Host "⚠️  Could not terminate WSL. Trying full shutdown..." -ForegroundColor Yellow
    wsl --shutdown
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ All WSL distributions shut down." -ForegroundColor Green
    } else {
        Write-Host "❌ Failed to restart WSL. You may need to run this as Administrator." -ForegroundColor Red
    }
}

Write-Host ""
Write-Host "After reconnecting, verify the fix:" -ForegroundColor Cyan
Write-Host "  which code" -ForegroundColor White
Write-Host "  code --version" -ForegroundColor White

