#!/bin/bash
# Explorer Status Diagnostic Script
# Checks all components of the explorer service

set -e

echo "=========================================="
echo "Explorer Status Diagnostic"
echo "=========================================="
echo ""

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

# Configuration
EXPLORER_VMID=5000
EXPLORER_IP="192.168.11.140"
EXPLORER_URL="https://explorer.d-bis.org"
PROXMOX_HOST="192.168.11.10"

echo "1. Checking Public URL Access..."
echo "----------------------------------------"
if curl -s -o /dev/null -w "%{http_code}" "$EXPLORER_URL" | grep -q "200\|301\|302"; then
    echo -e "${GREEN}✓${NC} Explorer URL is accessible: $EXPLORER_URL"
else
    HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "$EXPLORER_URL" 2>&1)
    echo -e "${RED}✗${NC} Explorer URL not accessible: $EXPLORER_URL (HTTP $HTTP_CODE)"
fi
echo ""

echo "2. Checking API Endpoint..."
echo "----------------------------------------"
API_RESPONSE=$(curl -s "$EXPLORER_URL/api/v2/stats" 2>&1)
if [ -n "$API_RESPONSE" ] && echo "$API_RESPONSE" | grep -q "chain_id\|block_number"; then
    echo -e "${GREEN}✓${NC} API endpoint responding"
    echo "$API_RESPONSE" | head -5
else
    echo -e "${RED}✗${NC} API endpoint not responding or empty"
    echo "Response: $API_RESPONSE"
fi
echo ""

echo "3. Checking Proxmox Container Status..."
echo "----------------------------------------"
if command -v pct &> /dev/null; then
    if pct list 2>/dev/null | grep -q "^\s*$EXPLORER_VMID\s"; then
        CONTAINER_STATUS=$(pct status $EXPLORER_VMID 2>/dev/null | awk '{print $2}')
        if [ "$CONTAINER_STATUS" = "running" ]; then
            echo -e "${GREEN}✓${NC} Container VMID $EXPLORER_VMID is running"
        else
            echo -e "${YELLOW}⚠${NC} Container VMID $EXPLORER_VMID exists but status: $CONTAINER_STATUS"
        fi
    else
        echo -e "${RED}✗${NC} Container VMID $EXPLORER_VMID not found"
    fi
else
    echo -e "${YELLOW}⚠${NC} pct command not available (not on Proxmox host)"
    echo "   Attempting SSH check to $PROXMOX_HOST..."
    if ssh -o ConnectTimeout=5 -o BatchMode=yes root@$PROXMOX_HOST "pct list 2>/dev/null | grep -q '^\s*$EXPLORER_VMID\s'" 2>/dev/null; then
        CONTAINER_STATUS=$(ssh root@$PROXMOX_HOST "pct status $EXPLORER_VMID 2>/dev/null | awk '{print \$2}'" 2>/dev/null)
        if [ "$CONTAINER_STATUS" = "running" ]; then
            echo -e "${GREEN}✓${NC} Container VMID $EXPLORER_VMID is running (via SSH)"
        else
            echo -e "${YELLOW}⚠${NC} Container VMID $EXPLORER_VMID exists but status: $CONTAINER_STATUS"
        fi
    else
        echo -e "${RED}✗${NC} Cannot check container status (SSH not available or container missing)"
    fi
fi
echo ""

echo "4. Checking Direct IP Access..."
echo "----------------------------------------"
if curl -s -o /dev/null -w "%{http_code}" "http://$EXPLORER_IP" 2>&1 | grep -q "200\|301\|302\|404"; then
    HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "http://$EXPLORER_IP" 2>&1)
    echo -e "${GREEN}✓${NC} Direct IP access responding: http://$EXPLORER_IP (HTTP $HTTP_CODE)"
else
    echo -e "${RED}✗${NC} Direct IP access not responding: http://$EXPLORER_IP"
fi
echo ""

echo "5. Checking Blockscout Port (4000)..."
echo "----------------------------------------"
if timeout 3 bash -c "echo > /dev/tcp/$EXPLORER_IP/4000" 2>/dev/null; then
    echo -e "${GREEN}✓${NC} Port 4000 is open on $EXPLORER_IP"
    BLOCKSCOUT_RESPONSE=$(curl -s "http://$EXPLORER_IP:4000/api/v2/status" 2>&1)
    if echo "$BLOCKSCOUT_RESPONSE" | grep -q "chain_id"; then
        echo -e "${GREEN}✓${NC} Blockscout API responding"
    else
        echo -e "${YELLOW}⚠${NC} Port open but Blockscout API not responding correctly"
    fi
else
    echo -e "${RED}✗${NC} Port 4000 is not accessible on $EXPLORER_IP"
fi
echo ""

echo "6. Checking Nginx Ports (80, 443)..."
echo "----------------------------------------"
for PORT in 80 443; do
    if timeout 3 bash -c "echo > /dev/tcp/$EXPLORER_IP/$PORT" 2>/dev/null; then
        echo -e "${GREEN}✓${NC} Port $PORT is open on $EXPLORER_IP"
    else
        echo -e "${RED}✗${NC} Port $PORT is not accessible on $EXPLORER_IP"
    fi
done
echo ""

echo "7. Checking Docker Containers (if accessible)..."
echo "----------------------------------------"
if ssh -o ConnectTimeout=5 -o BatchMode=yes root@$PROXMOX_HOST "pct exec $EXPLORER_VMID -- docker ps 2>/dev/null" 2>/dev/null | grep -q "blockscout\|postgres"; then
    echo -e "${GREEN}✓${NC} Docker containers found in container"
    ssh root@$PROXMOX_HOST "pct exec $EXPLORER_VMID -- docker ps 2>/dev/null" 2>/dev/null | grep -E "CONTAINER|blockscout|postgres" || true
else
    echo -e "${YELLOW}⚠${NC} Cannot check Docker containers (container not accessible or not running)"
fi
echo ""

echo "8. Checking explorer-monorepo Backend..."
echo "----------------------------------------"
if curl -s "http://localhost:8080/health" 2>&1 | grep -q "status\|healthy"; then
    echo -e "${GREEN}✓${NC} explorer-monorepo backend is running on port 8080"
else
    echo -e "${YELLOW}⚠${NC} explorer-monorepo backend not responding on port 8080"
    if pgrep -f "api-server" > /dev/null; then
        echo -e "${GREEN}✓${NC} But api-server process is running"
    else
        echo -e "${RED}✗${NC} api-server process not found"
    fi
fi
echo ""

echo "=========================================="
echo "Diagnostic Complete"
echo "=========================================="

