# Test the API endpoint directly
Write-Host "[OK] Testing API endpoints on AD2..." -ForegroundColor Green

# Test 1: Stats endpoint
Write-Host "`n[TEST 1] GET /api/stats" -ForegroundColor Cyan
try {
    $response = Invoke-WebRequest -Uri "http://192.168.0.6:3000/api/stats" -Method GET -UseBasicParsing -ErrorAction Stop
    Write-Host "[OK] Status: $($response.StatusCode)" -ForegroundColor Green
    Write-Host "[OK] Response:" -ForegroundColor Green
    $response.Content | ConvertFrom-Json | ConvertTo-Json -Depth 5
} catch {
    Write-Host "[ERROR] Request failed: $($_.Exception.Message)" -ForegroundColor Red
    Write-Host "[ERROR] Response: $($_.Exception.Response)" -ForegroundColor Red
    if ($_.ErrorDetails.Message) {
        Write-Host "[ERROR] Details: $($_.ErrorDetails.Message)" -ForegroundColor Red
    }
}

# Test 2: Search endpoint (simple)
Write-Host "`n[TEST 2] GET /api/search?limit=1" -ForegroundColor Cyan
try {
    $response = Invoke-WebRequest -Uri "http://192.168.0.6:3000/api/search?limit=1" -Method GET -UseBasicParsing -ErrorAction Stop
    Write-Host "[OK] Status: $($response.StatusCode)" -ForegroundColor Green
    Write-Host "[OK] Response:" -ForegroundColor Green
    $response.Content | ConvertFrom-Json | ConvertTo-Json -Depth 5
} catch {
    Write-Host "[ERROR] Request failed: $($_.Exception.Message)" -ForegroundColor Red
    if ($_.ErrorDetails.Message) {
        Write-Host "[ERROR] Details: $($_.ErrorDetails.Message)" -ForegroundColor Red
    }
}

# Test 3: Record by ID
Write-Host "`n[TEST 3] GET /api/record/1" -ForegroundColor Cyan
try {
    $response = Invoke-WebRequest -Uri "http://192.168.0.6:3000/api/record/1" -Method GET -UseBasicParsing -ErrorAction Stop
    Write-Host "[OK] Status: $($response.StatusCode)" -ForegroundColor Green
    Write-Host "[OK] Response:" -ForegroundColor Green
    $response.Content | ConvertFrom-Json | ConvertTo-Json -Depth 5
} catch {
    Write-Host "[ERROR] Request failed: $($_.Exception.Message)" -ForegroundColor Red
    if ($_.ErrorDetails.Message) {
        Write-Host "[ERROR] Details: $($_.ErrorDetails.Message)" -ForegroundColor Red
    }
}

Write-Host "`n[OK] Done" -ForegroundColor Green
