# Diagnose Docker Desktop engine issues
# Usage: npm run dev:diagnose-docker

$ErrorActionPreference = "Continue"
. (Join-Path $PSScriptRoot "lib.ps1")

Write-Host "=== Docker diagnose ===" -ForegroundColor Cyan

$drive = (Get-Item $script:DevConfig.ProjectRoot).PSDrive.Name
$freeGb = [math]::Round((Get-PSDrive $drive).Free / 1GB, 1)
Write-Host "Disk free on ${drive}: ${freeGb} GB"
if ($freeGb -lt 10) {
    Write-Host "[WARN] Low disk space often causes Docker engine 500 / IPC timeout" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "WSL distros:"
$distro = Get-DevWslDistro
if ($distro) {
    Write-Host "  dev distro: $distro"
} else {
    Write-Host "  [WARN] no Linux WSL distro (only docker-desktop?). Run: wsl --install -d Ubuntu-22.04" -ForegroundColor Yellow
}
wsl -l -v

Write-Host ""
Write-Host "Docker engine status: $(Get-DockerEngineStatus)"
$info = Invoke-DockerInfoSafe
if ($info.Output) {
    Write-Host $info.Output.Substring(0, [Math]::Min(800, $info.Output.Length))
}

Write-Host ""
Write-Host "Recommendation:" -ForegroundColor Cyan
Write-Host "  Docker Desktop IPC/WSL engine stuck is a known Windows issue."
Write-Host "  Do NOT use DockerCli -SwitchDaemon (makes it worse)."
Write-Host ""
Write-Host "  Permanent dev path (recommended):"
Write-Host "    npm run dev:infra:setup    # mysql+redis in WSL, no Docker Desktop"
Write-Host "    npm run dev:start          # DEV_INFRA_MODE=native"
Write-Host ""
Write-Host "  If you must fix Docker Desktop:"
Write-Host "    1. wsl --shutdown (admin)"
Write-Host "    2. Quit Docker Desktop, restart PC"
Write-Host "    3. Ensure C: has 20GB+ free (compact docker-desktop-data vhdx)"
Write-Host "    4. Update Docker Desktop + WSL: wsl --update"
