#!/bin/bash
# Verify Agents Script - checks that agents are responding
# Usage: ./verify_agents.sh

set -e

echo "=== AGENT E2E VERIFICATION ==="
echo ""

# 1. Check prober metrics
echo "1. Checking prober metrics..."
PROBER_METRICS=$(curl -s http://localhost:9108/metrics 2>/dev/null || echo "FAIL")
if echo "$PROBER_METRICS" | grep -q "agent_e2e_success"; then
    echo "   ✅ Prober metrics available"
    echo "$PROBER_METRICS" | grep "agent_e2e_success" | head -3
else
    echo "   ❌ Prober metrics NOT available (prober may not be running)"
fi

echo ""

# 2. Check Prometheus targets
echo "2. Checking Prometheus targets..."
PROM_TARGETS=$(curl -s http://localhost:9090/api/v1/targets 2>/dev/null || echo "FAIL")
if echo "$PROM_TARGETS" | grep -q "prober"; then
    echo "   ✅ Prober found in Prometheus targets"
else
    echo "   ⚠️ Prober not in Prometheus targets yet"
fi

echo ""

# 3. Direct agent ping test
echo "3. Testing /debug/agent_ping..."
PING_RESULT=$(curl -s -X POST http://localhost:9300/debug/agent_ping -H "Content-Type: application/json" -d '{}' 2>/dev/null || echo '{"error":"connection failed"}')
echo "   $PING_RESULT"

echo ""

# 4. Gateway health
echo "4. Gateway health..."
HEALTH=$(curl -s http://localhost:9300/health 2>/dev/null || echo '{"status":"error"}')
if echo "$HEALTH" | grep -q "healthy"; then
    echo "   ✅ Gateway healthy"
    echo "   $HEALTH" | head -c 200
else
    echo "   ❌ Gateway unhealthy: $HEALTH"
fi

echo ""
echo ""

# 5. Webhook test
echo "5. Testing webhook (Helion)..."
WEBHOOK=$(curl -s -X POST http://localhost:9300/helion/telegram/webhook \
    -H "Content-Type: application/json" \
    -d '{"update_id":1}' 2>/dev/null || echo '{"error":"failed"}')
echo "   $WEBHOOK"

echo ""
echo "=== VERIFICATION COMPLETE ==="
