#!/bin/bash
# Check Blockscout Logs and Status
# Helps diagnose why Blockscout is not running

set -e

VMID="${1:-5000}"
PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
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_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; }
log_error() { echo -e "${RED}[✗]${NC} $1"; }

# Function to execute command in container
exec_container() {
    ssh -o StrictHostKeyChecking=no root@"$PROXMOX_HOST" "pct exec $VMID -- bash -c '$1'" 2>&1
}

echo ""
log_info "Blockscout Logs and Status Check"
log_info "VMID: $VMID"
echo ""

# Check systemd service
log_info "1. Checking Blockscout systemd service..."
SERVICE_STATUS=$(exec_container "systemctl is-active blockscout 2>/dev/null || echo 'inactive'")
if [ "$SERVICE_STATUS" = "active" ]; then
    log_success "Blockscout service is active"
else
    log_warn "Blockscout service is $SERVICE_STATUS"
fi

SERVICE_ENABLED=$(exec_container "systemctl is-enabled blockscout 2>/dev/null || echo 'disabled'")
log_info "Service enabled: $SERVICE_ENABLED"
echo ""

# Check service logs
log_info "2. Recent Blockscout service logs:"
exec_container "journalctl -u blockscout -n 30 --no-pager" || log_warn "Could not retrieve service logs"
echo ""

# Check Docker containers
log_info "3. Checking Docker containers..."
DOCKER_PS=$(exec_container "docker ps -a 2>/dev/null" || echo "Docker not accessible")
if echo "$DOCKER_PS" | grep -q "blockscout\|postgres"; then
    log_info "Docker containers:"
    echo "$DOCKER_PS"
else
    log_warn "No Blockscout Docker containers found"
fi
echo ""

# Check docker-compose
log_info "4. Checking docker-compose status..."
if exec_container "test -f /opt/blockscout/docker-compose.yml"; then
    log_info "docker-compose.yml found"
    COMPOSE_PS=$(exec_container "cd /opt/blockscout && docker-compose ps 2>/dev/null" || echo "Could not check compose status")
    echo "$COMPOSE_PS"
    echo ""
    log_info "Recent docker-compose logs:"
    exec_container "cd /opt/blockscout && docker-compose logs --tail=30 2>/dev/null" || log_warn "Could not retrieve compose logs"
else
    log_warn "docker-compose.yml not found at /opt/blockscout/docker-compose.yml"
fi
echo ""

# Check port 4000
log_info "5. Checking port 4000..."
PORT_CHECK=$(exec_container "ss -tlnp | grep :4000 || echo 'Port 4000 not listening'")
if echo "$PORT_CHECK" | grep -q ":4000"; then
    log_success "Port 4000 is listening"
    echo "$PORT_CHECK"
else
    log_error "Port 4000 is not listening"
    echo "$PORT_CHECK"
fi
echo ""

# Check Blockscout API directly
log_info "6. Testing Blockscout API (from container)..."
API_RESPONSE=$(exec_container "timeout 5 curl -s http://127.0.0.1:4000/api/v2/status 2>&1" || echo "FAILED")
if echo "$API_RESPONSE" | grep -q "chain_id\|success"; then
    log_success "Blockscout API is responding"
    echo "$API_RESPONSE" | head -10
else
    log_error "Blockscout API is not responding"
    echo "Response: $API_RESPONSE"
fi
echo ""

# Check PostgreSQL
log_info "7. Checking PostgreSQL connection..."
PG_CHECK=$(exec_container "pg_isready -h localhost -p 5432 2>&1" || echo "PostgreSQL not accessible")
if echo "$PG_CHECK" | grep -q "accepting connections"; then
    log_success "PostgreSQL is accepting connections"
else
    log_warn "PostgreSQL may not be running"
    echo "$PG_CHECK"
fi
echo ""

log_info "Diagnostic complete!"
echo ""
log_info "To restart Blockscout, try:"
echo "  pct exec $VMID -- systemctl restart blockscout"
echo "  # OR"
echo "  pct exec $VMID -- cd /opt/blockscout && docker-compose restart"

