#Requires -RunAsAdministrator
$ErrorActionPreference = "Continue"
$Host.UI.RawUI.WindowTitle = "Diagnose Claude Proxy Control"
try { chcp 65001 | Out-Null; [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false) } catch {}
. (Join-Path $PSScriptRoot "_env.ps1")

Write-Host ""
Write-Host "============================================================" -ForegroundColor White
Write-Host "  Diagnose Claude Proxy Control (product)" -ForegroundColor Yellow
Write-Host "============================================================" -ForegroundColor White
Write-Host ("  Home: " + $ProductHome) -ForegroundColor DarkGray

$cfg = Join-Path $MergedDir "vpn-claude-config.json"
$log = Join-Path $MergedDir "vpn-claude-tun.log"
$proxyEnv = Join-Path $ProductHome "proxy.env"
$status = Join-Path $MergedDir "status.json"

function Ok($m){ Write-Host "  [OK]   $m" -ForegroundColor Green }
function Bad($m){ Write-Host "  [FAIL] $m" -ForegroundColor Red }
function Inf($m){ Write-Host "  [..]   $m" -ForegroundColor Gray }
function Wrn($m){ Write-Host "  [WARN] $m" -ForegroundColor Yellow }

Write-Host ""
Write-Host "==> Processes" -ForegroundColor Cyan
$ours = @(Get-CpcSingBoxProcesses)
$other = @(Get-OtherSingBoxProcesses)
$cl = @(Get-Process claude -ErrorAction SilentlyContinue)
if ($ours.Count) { Ok ("CPC sing-box x{0} pid={1}" -f $ours.Count, (($ours | ForEach-Object ProcessId) -join ',')) } else { Bad "CPC sing-box not running" }
if ($other.Count) { Wrn ("other sing-box x{0} (личный — не CPC)" -f $other.Count) }
if ($cl.Count) { Ok ("claude x{0}" -f $cl.Count) } else { Wrn "Claude not running" }

Write-Host ""
Write-Host "==> Tasks" -ForegroundColor Cyan
try {
  $t = Get-ScheduledTask -TaskName $TaskWatcher -ErrorAction Stop
  Ok ("$TaskWatcher = " + $t.State)
} catch { Bad "$TaskWatcher missing" }

Write-Host ""
Write-Host "==> TUN adapters" -ForegroundColor Cyan
$tunName = $null
if (Test-Path $status) {
  try { $tunName = [string]((Get-Content $status -Raw | ConvertFrom-Json).tun) } catch {}
}
$ads = Get-NetAdapter -ErrorAction SilentlyContinue | Where-Object {
  ($tunName -and $_.Name -eq $tunName) -or $_.Name -match '^cpc' -or $_.InterfaceDescription -match 'sing-tun|Wintun'
}
if (-not $ads) { Bad "no CPC TUN adapters" }
foreach ($a in @($ads)) {
  if ($a.Status -eq "Up") { Ok ($a.Name + " = Up") } else { Wrn ($a.Name + " = " + $a.Status) }
}

Write-Host ""
Write-Host "==> Squid live" -ForegroundColor Cyan
if (Test-Path $proxyEnv) {
  $m = @{}
  Get-Content $proxyEnv | ForEach-Object {
    if ($_ -match "=" -and $_ -notmatch "^\s*#") {
      $k, $v = $_.Split("=", 2)
      $m[$k.Trim()] = $v.Trim()
    }
  }
  $proxyUrl = "http://" + $m.PROXY_USER + ":" + $m.PROXY_PASS + "@" + $m.PROXY_HOST + ":" + $m.PROXY_PORT
  $hello = & curl.exe -s -m 20 -x $proxyUrl "https://api.anthropic.com/api/hello"
  if ($hello -match "hello") { Ok ("hello => " + $hello) } else { Bad ("hello => " + $hello) }
  $ip = (& curl.exe -s -m 20 -x $proxyUrl "https://ifconfig.me" | Out-String).Trim()
  if ($ip -eq $m.PROXY_HOST) { Ok ("egress => " + $ip) } else { Wrn ("egress => " + $ip + " (host=" + $m.PROXY_HOST + ")") }
} else { Bad "proxy.env missing — активируй ключ в приложении" }

Write-Host ""
Write-Host "==> Config / status" -ForegroundColor Cyan
if (Test-Path $cfg) { Ok ("config: " + $cfg) } else { Bad "vpn-claude-config.json missing" }
if (Test-Path $status) {
  Ok "status.json:"
  Get-Content $status | ForEach-Object { Write-Host ("    " + $_) -ForegroundColor DarkGray }
} else { Wrn "status.json missing" }

Write-Host ""
Write-Host "==> License" -ForegroundColor Cyan
$lic = Join-Path $ProductHome "license.json"
if (Test-Path $lic) { Ok "license.json present" } else { Bad "license.json missing" }

Write-Host ""
Write-Host "Enter to close..." -ForegroundColor DarkGray
if (-not $env:CLAUDE_PROXY_NOPAUSE) { [void][Console]::ReadLine() }
