#!/bin/bash
# Complete verification of explorer functionality
# Tests all access points and endpoints

set -e

EXPLORER_DOMAIN="explorer.d-bis.org"
EXPLORER_IP="192.168.11.140"

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

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

echo ""
log_info "═══════════════════════════════════════════════════════════"
log_info "  EXPLORER COMPLETE VERIFICATION"
log_info "═══════════════════════════════════════════════════════════"
echo ""

# Test 1: Direct Blockscout API
log_info "Test 1: Direct Blockscout API (port 4000)..."
HTTP_4000=$(curl -s -o /dev/null -w "%{http_code}" "http://$EXPLORER_IP:4000/api/v2/stats" 2>&1)
if [ "$HTTP_4000" = "200" ]; then
    log_success "Direct API: HTTP $HTTP_4000"
    RESPONSE=$(curl -s "http://$EXPLORER_IP:4000/api/v2/stats" 2>&1)
    if echo "$RESPONSE" | grep -q -E "total_blocks|chain_id"; then
        log_success "Valid JSON response"
        echo "$RESPONSE" | jq -r '.total_blocks, .total_transactions, .total_addresses' 2>/dev/null || echo "$RESPONSE" | head -3
    fi
else
    log_error "Direct API: HTTP $HTTP_4000"
fi

# Test 2: Nginx Proxy (HTTP)
log_info "Test 2: Nginx Proxy (HTTP)..."
HTTP_80=$(curl -s -o /dev/null -w "%{http_code}" "http://$EXPLORER_IP/api/v2/stats" 2>&1)
if [ "$HTTP_80" = "200" ]; then
    log_success "Nginx HTTP: HTTP $HTTP_80"
else
    log_error "Nginx HTTP: HTTP $HTTP_80"
fi

# Test 3: Nginx Proxy (HTTPS)
log_info "Test 3: Nginx Proxy (HTTPS)..."
HTTP_443=$(curl -s -k -o /dev/null -w "%{http_code}" "https://$EXPLORER_IP/api/v2/stats" 2>&1)
if [ "$HTTP_443" = "200" ]; then
    log_success "Nginx HTTPS: HTTP $HTTP_443"
else
    log_warn "Nginx HTTPS: HTTP $HTTP_443 (may not be configured)"
fi

# Test 4: Public URL (HTTPS via Cloudflare)
log_info "Test 4: Public URL via Cloudflare (HTTPS)..."
HTTP_PUBLIC=$(curl -s -o /dev/null -w "%{http_code}" "https://$EXPLORER_DOMAIN/api/v2/stats" 2>&1)
if [ "$HTTP_PUBLIC" = "200" ]; then
    log_success "Public URL: HTTP $HTTP_PUBLIC"
    RESPONSE=$(curl -s "https://$EXPLORER_DOMAIN/api/v2/stats" 2>&1)
    if echo "$RESPONSE" | grep -q -E "total_blocks|chain_id"; then
        log_success "Valid JSON response from public URL"
        echo "$RESPONSE" | jq -r '.total_blocks, .total_transactions, .total_addresses' 2>/dev/null || echo "$RESPONSE" | head -3
    fi
elif [ "$HTTP_PUBLIC" = "530" ]; then
    log_warn "Public URL: HTTP 530 (Cloudflare tunnel not connected)"
elif [ "$HTTP_PUBLIC" = "404" ]; then
    log_warn "Public URL: HTTP 404 (DNS/tunnel route issue)"
else
    log_warn "Public URL: HTTP $HTTP_PUBLIC"
fi

# Test 5: Frontend Page
log_info "Test 5: Frontend Page..."
HTTP_FRONTEND=$(curl -s -o /dev/null -w "%{http_code}" "https://$EXPLORER_DOMAIN/" 2>&1)
if [ "$HTTP_FRONTEND" = "200" ]; then
    log_success "Frontend: HTTP $HTTP_FRONTEND"
elif [ "$HTTP_FRONTEND" = "530" ]; then
    log_warn "Frontend: HTTP 530 (tunnel not connected)"
else
    log_warn "Frontend: HTTP $HTTP_FRONTEND"
fi

# Test 6: DNS Resolution
log_info "Test 6: DNS Resolution..."
DNS_RESULT=$(dig +short "$EXPLORER_DOMAIN" 2>/dev/null | head -1 || echo "")
if [ -n "$DNS_RESULT" ]; then
    log_success "DNS resolves to: $DNS_RESULT"
    if echo "$DNS_RESULT" | grep -qE "^(104\.|172\.64\.|172\.65\.|172\.66\.|172\.67\.)"; then
        log_success "DNS points to Cloudflare (proxied correctly)"
    fi
else
    log_warn "DNS not resolving"
fi

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

if [ "$HTTP_4000" = "200" ]; then
    log_success "✓ Blockscout service: Running"
else
    log_error "✗ Blockscout service: Not accessible"
fi

if [ "$HTTP_80" = "200" ]; then
    log_success "✓ Nginx proxy: Working"
else
    log_error "✗ Nginx proxy: Not working"
fi

if [ "$HTTP_PUBLIC" = "200" ]; then
    log_success "✓ Public URL: Working"
else
    log_warn "⚠ Public URL: HTTP $HTTP_PUBLIC"
fi

if [ -n "$DNS_RESULT" ]; then
    log_success "✓ DNS: Resolving"
else
    log_warn "⚠ DNS: Not resolving"
fi

echo ""
