#!/bin/bash
# Verify Explorer from pve2 - Run this AFTER exiting the container
# Usage: Run on pve2 node after Blockscout is started

set -e

EXPLORER_IP="192.168.11.140"
EXPLORER_URL="https://explorer.d-bis.org"

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[✓]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }

echo ""
log_info "═══════════════════════════════════════════════════════════"
log_info "  EXPLORER VERIFICATION FROM PVE2"
log_info "═══════════════════════════════════════════════════════════"
echo ""

# Test 1: Direct Blockscout API (port 4000)
log_info "Test 1: Direct Blockscout API (port 4000)"
if timeout 3 bash -c "echo > /dev/tcp/$EXPLORER_IP/4000" 2>/dev/null; then
    log_success "Port 4000 is accessible"
    API_RESPONSE=$(curl -s "http://$EXPLORER_IP:4000/api/v2/status" 2>&1)
    if echo "$API_RESPONSE" | grep -q -E "chain_id|success"; then
        log_success "API is responding correctly!"
        echo "$API_RESPONSE" | head -10
    else
        log_warn "Port open but API response unexpected"
        echo "Response: $API_RESPONSE"
    fi
else
    log_error "Port 4000 is not accessible"
fi
echo ""

# Test 2: Nginx Proxy (port 80)
log_info "Test 2: Nginx Proxy (port 80)"
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "http://$EXPLORER_IP/api/v2/stats" 2>&1)
if [ "$HTTP_CODE" = "200" ]; then
    log_success "Nginx proxy is working (HTTP 200)"
    PROXY_RESPONSE=$(curl -s "http://$EXPLORER_IP/api/v2/stats" 2>&1)
    if echo "$PROXY_RESPONSE" | grep -q -E "chain_id|block_number"; then
        log_success "Proxy returns valid API response"
        echo "$PROXY_RESPONSE" | head -10
    else
        log_warn "Proxy returns HTTP 200 but unexpected content"
    fi
elif [ "$HTTP_CODE" = "502" ]; then
    log_warn "Nginx proxy returns 502 Bad Gateway"
    log_info "Blockscout may still be starting, wait 30-60 seconds and retry"
else
    log_warn "Nginx proxy returns HTTP $HTTP_CODE"
fi
echo ""

# Test 3: HTTPS (port 443)
log_info "Test 3: HTTPS (port 443)"
if timeout 3 bash -c "echo > /dev/tcp/$EXPLORER_IP/443" 2>/dev/null; then
    log_success "Port 443 is accessible"
    HTTPS_RESPONSE=$(curl -s -k "https://$EXPLORER_IP/api/v2/stats" 2>&1)
    if echo "$HTTPS_RESPONSE" | grep -q -E "chain_id|block_number"; then
        log_success "HTTPS proxy is working"
    else
        log_warn "HTTPS accessible but response unexpected"
    fi
else
    log_warn "Port 443 is not accessible"
fi
echo ""

# Test 4: Public URL
log_info "Test 4: Public URL (explorer.d-bis.org)"
PUBLIC_HTTP=$(curl -s -o /dev/null -w "%{http_code}" "$EXPLORER_URL/api/v2/stats" 2>&1)
if [ "$PUBLIC_HTTP" = "200" ]; then
    log_success "Public URL is accessible (HTTP 200)"
    PUBLIC_RESPONSE=$(curl -s "$EXPLORER_URL/api/v2/stats" 2>&1)
    if echo "$PUBLIC_RESPONSE" | grep -q -E "chain_id|block_number"; then
        log_success "Public URL returns valid API response"
    fi
elif [ "$PUBLIC_HTTP" = "404" ]; then
    log_warn "Public URL returns 404"
    log_info "Check Cloudflare DNS and tunnel configuration"
elif [ "$PUBLIC_HTTP" = "502" ]; then
    log_warn "Public URL returns 502"
    log_info "Cloudflare tunnel may not be routing correctly"
else
    log_warn "Public URL returns HTTP $PUBLIC_HTTP"
fi
echo ""

# Summary
log_info "═══════════════════════════════════════════════════════════"
log_info "  VERIFICATION SUMMARY"
log_info "═══════════════════════════════════════════════════════════"
echo ""

ALL_TESTS_PASS=true

# Check each test
if timeout 3 bash -c "echo > /dev/tcp/$EXPLORER_IP/4000" 2>/dev/null; then
    log_success "✓ Port 4000: Accessible"
else
    log_error "✗ Port 4000: Not accessible"
    ALL_TESTS_PASS=false
fi

HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "http://$EXPLORER_IP/api/v2/stats" 2>&1)
if [ "$HTTP_CODE" = "200" ]; then
    log_success "✓ Nginx Proxy: Working"
else
    log_warn "✗ Nginx Proxy: HTTP $HTTP_CODE"
    if [ "$HTTP_CODE" != "502" ]; then
        ALL_TESTS_PASS=false
    fi
fi

PUBLIC_HTTP=$(curl -s -o /dev/null -w "%{http_code}" "$EXPLORER_URL/api/v2/stats" 2>&1)
if [ "$PUBLIC_HTTP" = "200" ]; then
    log_success "✓ Public URL: Working"
else
    log_warn "✗ Public URL: HTTP $PUBLIC_HTTP"
fi

echo ""
if [ "$ALL_TESTS_PASS" = true ]; then
    log_success "All critical tests passed!"
else
    log_warn "Some tests failed - check logs and configuration"
fi
echo ""

