$report = "claude-code-diagnostic-PROBLEMA.md"
$out = @()
$out += "# Claude Code Diagnostic - MASINA CU PROBLEME - $env:COMPUTERNAME - $(Get-Date -Format 'yyyy-MM-dd HH:mm')"
$out += ""

$out += "## 1. Versiuni si instalare"
$out += '```'
$out += "claude --version: $(claude --version 2>&1)"
$out += "where claude   : $((where.exe claude 2>&1) -join '; ')"
$out += "node --version : $(node --version 2>&1)"
$out += "npm --version  : $(npm --version 2>&1)"
$out += "where node     : $((where.exe node 2>&1) -join '; ')"
$out += "where npm      : $((where.exe npm 2>&1) -join '; ')"
$out += ""
$out += "npm global claude pkg:"
$out += (npm list -g --depth=0 2>$null | Select-String -Pattern "claude" | Out-String).Trim()
$out += ""
$out += "Native installer existence:"
$out += "  ~\.local\bin\claude.exe        : $(Test-Path "$env:USERPROFILE\.local\bin\claude.exe")"
$out += "  LOCALAPPDATA\Programs\claude\  : $(Test-Path "$env:LOCALAPPDATA\Programs\claude\claude.exe")"
$out += "  ~\.claude\local\               : $(Test-Path "$env:USERPROFILE\.claude\local")"
$out += '```'
$out += ""

$out += "## 2. claude doctor"
$out += '```'
$out += "RULEAZA MANUAL: claude doctor"
$out += "(nu se poate captura non-interactiv - copiaza output-ul aici manual)"
$out += '```'
$out += ""

$out += "## 3. Configuratie Claude"
$out += '```'
if (Test-Path "$env:USERPROFILE\.claude.json") {
    try {
        $cfg = Get-Content "$env:USERPROFILE\.claude.json" -Raw | ConvertFrom-Json
        $out += "~/.claude.json keys:"
        $out += ($cfg.PSObject.Properties.Name -join ", ")
        $out += ""
        $out += "installMethod: $($cfg.installMethod)"
        $out += "autoUpdates  : $($cfg.autoUpdates)"
        $out += "autoUpdatesProtectedForNative: $($cfg.autoUpdatesProtectedForNative)"
    } catch {
        $out += "ERROR parsing .claude.json: $_"
    }
} else {
    $out += "~/.claude.json: MISSING"
}
$out += ""
$out += "Settings files:"
$out += "  ~/.claude/settings.json       : $(Test-Path "$env:USERPROFILE\.claude\settings.json")"
$out += "  ./.claude/settings.json       : $(Test-Path ".\.claude\settings.json")"
$out += "  ./.claude/settings.local.json : $(Test-Path ".\.claude\settings.local.json")"
$out += ""
$out += "Credentials file: $(Test-Path "$env:USERPROFILE\.claude\.credentials.json")"
if (Test-Path "$env:USERPROFILE\.claude\.credentials.json") {
    $credSize = (Get-Item "$env:USERPROFILE\.claude\.credentials.json").Length
    $out += "  size: $credSize bytes"
}
$out += '```'
$out += ""

$out += "## 4. Variabile de mediu"
$out += '```'
$envs = Get-ChildItem Env: | Where-Object { $_.Name -match '^(CLAUDE_|ANTHROPIC_|NODE_|HTTP_PROXY|HTTPS_PROXY|NO_PROXY|ALL_PROXY)' }
if ($envs) {
    $out += ($envs | Format-Table -AutoSize | Out-String).Trim()
} else {
    $out += "(none set)"
}
$out += '```'
$out += ""

$out += "## 5. Retea"
$out += '```'
$out += "DNS servers:"
$out += (Get-DnsClientServerAddress -AddressFamily IPv4 | Where-Object {$_.ServerAddresses} | Format-Table InterfaceAlias, ServerAddresses -AutoSize | Out-String).Trim()
$out += ""
$out += "WinHTTP proxy:"
$out += (netsh winhttp show proxy | Out-String).Trim()
$out += ""
$out += "IE/system proxy:"
$ie = Get-ItemProperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -ErrorAction SilentlyContinue
$out += "  ProxyEnable  : $($ie.ProxyEnable)"
$out += "  ProxyServer  : $($ie.ProxyServer)"
$out += "  AutoConfigURL: $($ie.AutoConfigURL)"
$out += ""
$out += "DNS resolution api.anthropic.com:"
$out += "  A    : $((Resolve-DnsName api.anthropic.com -Type A -ErrorAction SilentlyContinue).IPAddress -join ', ')"
$out += "  AAAA : $((Resolve-DnsName api.anthropic.com -Type AAAA -ErrorAction SilentlyContinue).IPAddress -join ', ')"
$out += ""
$out += "TCP 443 to api.anthropic.com:"
$tcp = Test-NetConnection -ComputerName api.anthropic.com -Port 443 -WarningAction SilentlyContinue
$out += "  RemoteAddress    : $($tcp.RemoteAddress)"
$out += "  TcpTestSucceeded : $($tcp.TcpTestSucceeded)"
$out += ""
$out += "HTTP test (POST /v1/messages, expect 401):"
try {
    $resp = Invoke-WebRequest -Uri https://api.anthropic.com/v1/messages -Method POST -UseBasicParsing -TimeoutSec 10 -ErrorAction Stop
    $out += "  Status: $($resp.StatusCode) - UNEXPECTED (should be 401)"
} catch {
    $sc = $_.Exception.Response.StatusCode.value__
    if ($sc) {
        $out += "  Status: $sc $($_.Exception.Response.StatusCode) (401 = OK / a ajuns la API)"
    } else {
        $out += "  ERROR: $($_.Exception.Message)"
    }
}
$out += '```'
$out += ""

$out += "## 6. Securitate"
$out += '```'
$out += "Antivirus products:"
try {
    $av = Get-CimInstance -Namespace root/SecurityCenter2 -ClassName AntiVirusProduct -ErrorAction Stop
    $out += ($av | Select-Object displayName, productState | Format-Table -AutoSize | Out-String).Trim()
} catch {
    $out += "  (could not query - $_)"
}
$out += ""
$out += "Defender state:"
try {
    $mp = Get-MpPreference
    $out += "  DisableRealtimeMonitoring: $($mp.DisableRealtimeMonitoring)"
    $out += "  EnableNetworkProtection  : $($mp.EnableNetworkProtection)"
    $st = Get-MpComputerStatus
    $out += "  AntivirusEnabled         : $($st.AntivirusEnabled)"
    $out += "  RealTimeProtectionEnabled: $($st.RealTimeProtectionEnabled)"
} catch {
    $out += "  (need admin for full info)"
}
$out += ""
$out += "Firewall rules claude:"
$fwc = Get-NetFirewallRule -DisplayName "*claude*" -ErrorAction SilentlyContinue
if ($fwc) {
    $out += ($fwc | Select-Object DisplayName, Enabled, Direction, Action | Format-Table -AutoSize | Out-String).Trim()
} else {
    $out += "  No claude rules"
}
$out += ""
$out += "Firewall rules node:"
$fwn = Get-NetFirewallRule -DisplayName "*node*" -ErrorAction SilentlyContinue
if ($fwn) {
    $out += ($fwn | Select-Object DisplayName, Enabled, Direction, Action | Format-Table -AutoSize | Out-String).Trim()
} else {
    $out += "  No node rules"
}
$out += '```'
$out += ""

$out += "## 7. Sistem"
$out += '```'
$ver = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion'
$out += "OS DisplayVersion: $($ver.DisplayVersion)"
$out += "Build           : $($ver.CurrentBuild).$($ver.UBR)"
$out += "PowerShell      : $($PSVersionTable.PSVersion)"
$out += "Architecture    : $env:PROCESSOR_ARCHITECTURE"
$out += ""
$out += "Network profiles:"
$out += (Get-NetConnectionProfile | Select-Object Name, NetworkCategory, IPv4Connectivity, IPv6Connectivity | Format-Table -AutoSize | Out-String).Trim()
$out += ""
$out += "VPN-like adapters:"
$vpn = Get-NetAdapter | Where-Object { $_.InterfaceDescription -match 'VPN|TAP|WireGuard|Tailscale|ZeroTier|WARP|OpenVPN' }
if ($vpn) {
    $out += ($vpn | Select-Object Name, InterfaceDescription, Status | Format-Table -AutoSize | Out-String).Trim()
} else {
    $out += "  none"
}
$out += '```'
$out += ""

$out += "## 8. Test minim Claude Code (CAPTUREAZA RETRY LOOP)"
$out += '```'
$out += "RULEAZA MANUAL in alt terminal si lipeste output-ul aici:"
$out += "  claude --debug"
$out += "Apoi tasteaza: buna"
$out += "Asteapta 1-2 retries, apoi Ctrl+C si copiaza ULTIMELE 30-40 linii."
$out += '```'

$out -join "`r`n" | Out-File -FilePath $report -Encoding UTF8
Write-Host ""
Write-Host "Report written to: $report" -ForegroundColor Green
Write-Host "Locatie completa : $((Resolve-Path $report).Path)" -ForegroundColor Green