#!/usr/bin/env bash
# Check Blockscout status on VMID 5000 (pve2)
# Usage: ./check-blockscout-status.sh

VMID=5000
BLOCKSCOUT_URL="https://explorer.d-bis.org"
BLOCKSCOUT_API="${BLOCKSCOUT_URL}/api"

echo "========================================="
echo "Blockscout Status Check"
echo "VMID: $VMID (pve2)"
echo "URL: $BLOCKSCOUT_URL"
echo "========================================="
echo ""

# Check if container exists (run from Proxmox host)
if command -v pct &>/dev/null; then
    echo "1. Checking container status..."
    CONTAINER_STATUS=$(pct status $VMID 2>/dev/null || echo "not found")
    if [[ "$CONTAINER_STATUS" == *"running"* ]]; then
        echo "   ✅ Container is running"
        
        # Check Blockscout service inside container
        echo ""
        echo "2. Checking Blockscout service..."
        SERVICE_STATUS=$(pct exec $VMID -- systemctl is-active blockscout.service 2>/dev/null || echo "inactive")
        if [[ "$SERVICE_STATUS" == "active" ]]; then
            echo "   ✅ Blockscout service is active"
        else
            echo "   ⚠️  Blockscout service is $SERVICE_STATUS"
            echo "   💡 Start with: pct exec $VMID -- systemctl start blockscout"
        fi
        
        # Check docker containers
        echo ""
        echo "3. Checking Docker containers..."
        DOCKER_PS=$(pct exec $VMID -- docker ps --format "{{.Names}}: {{.Status}}" 2>/dev/null || echo "")
        if echo "$DOCKER_PS" | grep -q blockscout; then
            echo "   ✅ Blockscout containers running:"
            echo "$DOCKER_PS" | grep blockscout | sed 's/^/      /'
        else
            echo "   ⚠️  No Blockscout containers running"
        fi
    else
        echo "   ⚠️  Container status: $CONTAINER_STATUS"
        echo "   💡 Start container: pct start $VMID"
    fi
else
    echo "   ⚠️  pct command not available (not on Proxmox host)"
fi

# Check API accessibility
echo ""
echo "4. Checking API accessibility..."
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 "$BLOCKSCOUT_API" 2>/dev/null || echo "000")
if [[ "$HTTP_CODE" == "200" ]]; then
    echo "   ✅ API is accessible (HTTP $HTTP_CODE)"
elif [[ "$HTTP_CODE" == "502" ]]; then
    echo "   ⚠️  API returns 502 Bad Gateway (service may be down)"
    echo "   💡 Check Blockscout service: pct exec $VMID -- systemctl status blockscout"
elif [[ "$HTTP_CODE" == "000" ]]; then
    echo "   ❌ API is not accessible (connection timeout/failed)"
else
    echo "   ⚠️  API returned HTTP $HTTP_CODE"
fi

echo ""
echo "========================================="
echo "Summary"
echo "========================================="
echo "Blockscout URL: $BLOCKSCOUT_URL"
echo "API Endpoint: $BLOCKSCOUT_API"
echo "Container: $VMID (pve2)"
echo ""
echo "To start Blockscout:"
echo "  pct exec $VMID -- systemctl start blockscout"
echo ""
echo "To check service status:"
echo "  pct exec $VMID -- systemctl status blockscout"
echo ""
echo "To check logs:"
echo "  pct exec $VMID -- journalctl -u blockscout -n 50"
