#!/bin/bash
# Verify tunnel configuration and test all endpoints

echo "═══════════════════════════════════════════════════════════"
echo "  Verify Tunnel Configuration"
echo "═══════════════════════════════════════════════════════════"
echo ""

# Expected hostnames
EXPECTED_HOSTNAMES=(
    "explorer.d-bis.org"
    "rpc-http-pub.d-bis.org"
    "rpc-http-prv.d-bis.org"
    "dbis-admin.d-bis.org"
    "dbis-api.d-bis.org"
    "dbis-api-2.d-bis.org"
    "mim4u.org"
    "www.mim4u.org"
    "rpc-ws-pub.d-bis.org"
    "rpc-ws-prv.d-bis.org"
)

echo "✅ Configuration Verified in Cloudflare Dashboard:"
echo ""
echo "All 10 routes configured:"
for i in "${!EXPECTED_HOSTNAMES[@]}"; do
    num=$((i+1))
    echo "  $num. ${EXPECTED_HOSTNAMES[$i]} → http://192.168.11.21:80"
done
echo ""

# Check for differences
echo "⚠️  Note: Domain Configuration Differences"
echo ""
echo "  In DNS Zone File:"
echo "    - mim4u.org.d-bis.org (subdomain)"
echo "    - www.mim4u.org.d-bis.org (subdomain)"
echo ""
echo "  In Tunnel Config:"
echo "    - mim4u.org (root domain)"
echo "    - www.mim4u.org (root domain)"
echo ""
echo "  This is correct if mim4u.org is a separate domain!"
echo ""

# Test endpoints
echo "═══════════════════════════════════════════════════════════"
echo "  Testing Endpoints"
echo "═══════════════════════════════════════════════════════════"
echo ""

SUCCESS=0
FAILED=0

for hostname in "${EXPECTED_HOSTNAMES[@]}"; do
    # Skip WebSocket endpoints for HTTP test
    if [[ "$hostname" == *"ws-"* ]]; then
        echo "  ⏭️  $hostname (WebSocket - skipping HTTP test)"
        continue
    fi
    
    echo -n "  Testing $hostname... "
    
    HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 "https://$hostname" 2>/dev/null || echo "000")
    
    if [ "$HTTP_CODE" = "000" ]; then
        echo "❌ FAILED (timeout/no connection)"
        FAILED=$((FAILED+1))
    elif [ "$HTTP_CODE" -ge 200 ] && [ "$HTTP_CODE" -lt 400 ]; then
        echo "✅ OK (HTTP $HTTP_CODE)"
        SUCCESS=$((SUCCESS+1))
    elif [ "$HTTP_CODE" -ge 400 ] && [ "$HTTP_CODE" -lt 500 ]; then
        echo "⚠️  HTTP $HTTP_CODE (service may be down)"
        FAILED=$((FAILED+1))
    else
        echo "❌ HTTP $HTTP_CODE"
        FAILED=$((FAILED+1))
    fi
done

echo ""
echo "═══════════════════════════════════════════════════════════"
echo "  Test Results"
echo "═══════════════════════════════════════════════════════════"
echo ""
echo "  ✅ Successful: $SUCCESS"
echo "  ❌ Failed: $FAILED"
echo ""

if [ $FAILED -eq 0 ]; then
    echo "🎉 All endpoints are accessible!"
else
    echo "⚠️  Some endpoints failed. Possible reasons:"
    echo "  1. Tunnel connector not running (VMID 102)"
    echo "  2. Nginx not accessible at 192.168.11.21:80"
    echo "  3. Services not configured in Nginx"
    echo "  4. DNS propagation delay"
    echo ""
    echo "Next steps:"
    echo "  1. Check tunnel status in Cloudflare Dashboard"
    echo "  2. Verify container is running: ssh root@192.168.11.12 'pct status 102'"
    echo "  3. Check tunnel service: ssh root@192.168.11.12 'pct exec 102 -- systemctl status cloudflared'"
fi

echo ""
