# PowerShell script to test API endpoints
# Run this from the server to diagnose issues

Write-Host "=== API Health Check Test ===" -ForegroundColor Cyan
Write-Host ""

# Test 1: Localhost HTTP
Write-Host "1. Testing localhost HTTP..." -ForegroundColor Yellow
try {
    $response = Invoke-WebRequest -Uri "http://localhost/health" -UseBasicParsing -ErrorAction Stop
    Write-Host "   ✓ HTTP localhost works: $($response.StatusCode)" -ForegroundColor Green
    Write-Host "   Response: $($response.Content)" -ForegroundColor Gray
} catch {
    Write-Host "   ✗ HTTP localhost failed: $($_.Exception.Message)" -ForegroundColor Red
}

# Test 2: Localhost HTTPS
Write-Host ""
Write-Host "2. Testing localhost HTTPS..." -ForegroundColor Yellow
try {
    $response = Invoke-WebRequest -Uri "https://localhost/health" -UseBasicParsing -SkipCertificateCheck -ErrorAction Stop
    Write-Host "   ✓ HTTPS localhost works: $($response.StatusCode)" -ForegroundColor Green
    Write-Host "   Response: $($response.Content)" -ForegroundColor Gray
} catch {
    Write-Host "   ✗ HTTPS localhost failed: $($_.Exception.Message)" -ForegroundColor Red
}

# Test 3: Domain HTTPS
Write-Host ""
Write-Host "3. Testing domain HTTPS..." -ForegroundColor Yellow
try {
    $response = Invoke-WebRequest -Uri "https://ramerpapi.appsinlive.in/health" -UseBasicParsing -ErrorAction Stop
    Write-Host "   ✓ Domain HTTPS works: $($response.StatusCode)" -ForegroundColor Green
    Write-Host "   Response: $($response.Content)" -ForegroundColor Gray
} catch {
    Write-Host "   ✗ Domain HTTPS failed: $($_.Exception.Message)" -ForegroundColor Red
}

# Test 4: API endpoint
Write-Host ""
Write-Host "4. Testing API endpoint..." -ForegroundColor Yellow
try {
    $response = Invoke-WebRequest -Uri "https://ramerpapi.appsinlive.in/api/v1/health" -UseBasicParsing -ErrorAction Stop
    Write-Host "   ✓ API endpoint works: $($response.StatusCode)" -ForegroundColor Green
    Write-Host "   Response: $($response.Content)" -ForegroundColor Gray
} catch {
    Write-Host "   ✗ API endpoint failed: $($_.Exception.Message)" -ForegroundColor Red
}

# Test 5: Check if Node.js process is running
Write-Host ""
Write-Host "5. Checking Node.js processes..." -ForegroundColor Yellow
$nodeProcesses = Get-Process -Name "node" -ErrorAction SilentlyContinue
if ($nodeProcesses) {
    Write-Host "   ✓ Node.js processes found: $($nodeProcesses.Count)" -ForegroundColor Green
    foreach ($proc in $nodeProcesses) {
        Write-Host "     - PID: $($proc.Id), CPU: $($proc.CPU), Memory: $([math]::Round($proc.WS/1MB, 2)) MB" -ForegroundColor Gray
    }
} else {
    Write-Host "   ✗ No Node.js processes found" -ForegroundColor Red
}

# Test 6: Check IIS Site Status
Write-Host ""
Write-Host "6. Checking IIS Site Status..." -ForegroundColor Yellow
try {
    Import-Module WebAdministration -ErrorAction Stop
    $site = Get-WebSite -Name "ramerpapi" -ErrorAction SilentlyContinue
    if ($site) {
        Write-Host "   ✓ IIS Site found: $($site.Name)" -ForegroundColor Green
        Write-Host "     State: $($site.State)" -ForegroundColor Gray
        Write-Host "     Physical Path: $($site.physicalPath)" -ForegroundColor Gray
    } else {
        Write-Host "   ⚠ IIS Site 'ramerpapi' not found. Checking all sites..." -ForegroundColor Yellow
        $allSites = Get-WebSite
        foreach ($s in $allSites) {
            Write-Host "     - $($s.Name): $($s.State)" -ForegroundColor Gray
        }
    }
} catch {
    Write-Host "   ✗ Could not check IIS: $($_.Exception.Message)" -ForegroundColor Red
}

# Test 7: Check Port Binding
Write-Host ""
Write-Host "7. Checking port bindings..." -ForegroundColor Yellow
try {
    $bindings = Get-WebBinding | Where-Object { $_.bindingInformation -like "*443*" -or $_.bindingInformation -like "*80*" }
    if ($bindings) {
        Write-Host "   ✓ Found bindings:" -ForegroundColor Green
        foreach ($binding in $bindings) {
            Write-Host "     - $($binding.protocol)://$($binding.bindingInformation)" -ForegroundColor Gray
        }
    } else {
        Write-Host "   ⚠ No HTTP/HTTPS bindings found" -ForegroundColor Yellow
    }
} catch {
    Write-Host "   ✗ Could not check bindings: $($_.Exception.Message)" -ForegroundColor Red
}

# Test 8: Check Firewall
Write-Host ""
Write-Host "8. Checking Windows Firewall rules..." -ForegroundColor Yellow
try {
    $firewallRules = Get-NetFirewallRule | Where-Object { $_.DisplayName -like "*IIS*" -or $_.DisplayName -like "*HTTP*" -or $_.DisplayName -like "*HTTPS*" } | Select-Object DisplayName, Enabled, Direction
    if ($firewallRules) {
        Write-Host "   ✓ Firewall rules found:" -ForegroundColor Green
        foreach ($rule in $firewallRules) {
            $status = if ($rule.Enabled) { "Enabled" } else { "Disabled" }
            Write-Host "     - $($rule.DisplayName): $status ($($rule.Direction))" -ForegroundColor Gray
        }
    } else {
        Write-Host "   ⚠ No relevant firewall rules found" -ForegroundColor Yellow
    }
} catch {
    Write-Host "   ✗ Could not check firewall: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host ""
Write-Host "=== Test Complete ===" -ForegroundColor Cyan

