#!/bin/bash

# Script to check Blockscout status and remaining issues
# Run from Proxmox host or inside VMID 5000

set -euo pipefail

VMID=5000

echo "=========================================="
echo "Blockscout Status Check"
echo "=========================================="
echo ""

# Check if running from Proxmox host or inside container
if [ -f "/proc/1/cgroup" ] && grep -q "lxc" /proc/1/cgroup 2>/dev/null; then
    EXEC_PREFIX=""
    echo "Running inside VMID 5000"
else
    EXEC_PREFIX="pct exec $VMID --"
    echo "Running from Proxmox host, executing in VMID 5000"
fi

# 1. Check database status
echo "=== 1. Database Status ==="
$EXEC_PREFIX docker exec -it blockscout-postgres psql -U blockscout -d blockscout -c "
SELECT 
  CASE WHEN EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'blocks') 
    THEN '✅ blocks' ELSE '❌ blocks MISSING' END as blocks,
  CASE WHEN EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'transactions') 
    THEN '✅ transactions' ELSE '❌ transactions MISSING' END as transactions,
  CASE WHEN EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'migrations_status') 
    THEN '✅ migrations_status' ELSE '❌ migrations_status MISSING' END as migrations_status;
" 2>/dev/null
echo ""

# 2. Check Blockscout container status
echo "=== 2. Blockscout Container Status ==="
$EXEC_PREFIX docker ps -a | grep blockscout | grep -v postgres || echo "❌ Blockscout container not found"
echo ""

# 3. Check if static assets exist
echo "=== 3. Static Assets Check ==="
BLOCKSCOUT_CONTAINER=$($EXEC_PREFIX docker ps -a | grep blockscout | grep -v postgres | awk '{print $1}' | head -1)
if [ -n "$BLOCKSCOUT_CONTAINER" ]; then
    echo "Checking for cache_manifest.json..."
    $EXEC_PREFIX docker exec -it $BLOCKSCOUT_CONTAINER test -f priv/static/cache_manifest.json 2>/dev/null && \
      echo "✅ cache_manifest.json exists" || \
      echo "❌ cache_manifest.json MISSING - assets need to be built"
    
    echo "Checking static directory..."
    $EXEC_PREFIX docker exec -it $BLOCKSCOUT_CONTAINER ls -la priv/static/ 2>/dev/null | head -5 || \
      echo "⚠️  Cannot access static directory"
else
    echo "⚠️  Cannot check assets - Blockscout container not running"
fi
echo ""

# 4. Check docker-compose configuration
echo "=== 4. Docker Compose Configuration ==="
if $EXEC_PREFIX test -f /opt/blockscout/docker-compose.yml; then
    echo "docker-compose.yml found"
    if $EXEC_PREFIX grep -q "command:.*blockscout start" /opt/blockscout/docker-compose.yml; then
        echo "✅ Startup command configured correctly"
    else
        echo "❌ Startup command missing - needs: command: bin/blockscout start"
    fi
else
    echo "⚠️  docker-compose.yml not found at /opt/blockscout"
fi
echo ""

# 5. Check recent logs
echo "=== 5. Recent Logs (last 20 lines) ==="
if [ -n "$BLOCKSCOUT_CONTAINER" ]; then
    $EXEC_PREFIX docker logs $BLOCKSCOUT_CONTAINER 2>&1 | tail -20 || echo "Cannot access logs"
else
    echo "⚠️  Cannot check logs - container not found"
fi
echo ""

# 6. Check if Blockscout is responding
echo "=== 6. HTTP Endpoint Check ==="
if $EXEC_PREFIX docker ps | grep -q blockscout; then
    $EXEC_PREFIX curl -s http://localhost:4000/api/v2/stats 2>/dev/null | head -5 && \
      echo "✅ Blockscout API responding" || \
      echo "❌ Blockscout API not responding"
else
    echo "⚠️  Blockscout container not running"
fi
echo ""

echo "=========================================="
echo "Status Check Complete"
echo "=========================================="

