$AppDir = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
$EnvFile = Join-Path $AppDir "config\agent.env"
$Exe = Join-Path $AppDir "bin\controlcmd-agent.exe"

Write-Host "ControlCmd Windows Agent doctor"
Write-Host "App dir: $AppDir"
Write-Host "Executable: $(Test-Path $Exe)"
Write-Host "Config: $(Test-Path $EnvFile)"
Write-Host "Windows session id: $([System.Diagnostics.Process]::GetCurrentProcess().SessionId)"

foreach ($Shell in @("pwsh.exe", "powershell.exe", "cmd.exe")) {
  $Found = Get-Command $Shell -ErrorAction SilentlyContinue
  if ($Found) {
    Write-Host "Shell available: $Shell -> $($Found.Source)"
  }
}

if (Test-Path $EnvFile) {
  $ServerLine = Get-Content $EnvFile | Where-Object { $_ -match "^SERVER_URL=" } | Select-Object -First 1
  if ($ServerLine) {
    $ServerUrl = $ServerLine.Split("=", 2)[1].Trim()
    try {
      $Response = Invoke-WebRequest -UseBasicParsing -Uri "$ServerUrl/healthz" -TimeoutSec 10
      Write-Host "Server health: $($Response.StatusCode)"
    } catch {
      Write-Host "Server health failed: $($_.Exception.Message)"
    }
  }
}
