#!/bin/bash
# Start Blockscout Service - Run this script ON the Proxmox host
# Usage: Run directly on Proxmox host or via: ssh root@192.168.11.10 'bash -s' < start-blockscout-on-proxmox.sh

set -e

VMID=5000

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
RED='\033[0;31m'
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"; }

echo ""
log_info "Starting Blockscout Service in Container $VMID"
echo ""

# Find container node
CONTAINER_NODE=""
for node in ml110 pve pve2; do
    if pvesh get /nodes/$node/lxc/$VMID/status/current --output-format json >/dev/null 2>&1; then
        CONTAINER_NODE="$node"
        break
    fi
done

if [ -z "$CONTAINER_NODE" ]; then
    log_error "Container $VMID not found on any node"
    exit 1
fi

log_success "Container found on node: $CONTAINER_NODE"

# Check if we need to SSH to the node
if [ "$CONTAINER_NODE" != "$(hostname -s)" ]; then
    log_info "Container is on node $CONTAINER_NODE, executing commands via SSH..."
    NODE_IP=$(pvesh get /nodes/$CONTAINER_NODE --output-format json | grep -o '"ip":"[^"]*"' | head -1 | cut -d'"' -f4 || echo "")
    if [ -n "$NODE_IP" ]; then
        ssh root@$NODE_IP "pct exec $VMID -- bash -c 'systemctl start blockscout && sleep 5 && systemctl status blockscout --no-pager -l | head -10'"
    else
        # Try direct hostname
        ssh root@$CONTAINER_NODE "pct exec $VMID -- bash -c 'systemctl start blockscout && sleep 5 && systemctl status blockscout --no-pager -l | head -10'"
    fi
else
    # Running on same node
    log_info "Executing commands directly..."
    pct exec $VMID -- bash -c "systemctl start blockscout && sleep 5 && systemctl status blockscout --no-pager -l | head -10"
fi

# Wait and check
log_info "Waiting for Blockscout to start..."
sleep 15

# Check status
log_info "Checking Blockscout status..."
if [ "$CONTAINER_NODE" != "$(hostname -s)" ]; then
    NODE_IP=$(pvesh get /nodes/$CONTAINER_NODE --output-format json | grep -o '"ip":"[^"]*"' | head -1 | cut -d'"' -f4 || echo "")
    if [ -n "$NODE_IP" ]; then
        API_RESPONSE=$(ssh root@$NODE_IP "pct exec $VMID -- timeout 10 curl -s http://127.0.0.1:4000/api/v2/status 2>&1" || echo "FAILED")
    else
        API_RESPONSE=$(ssh root@$CONTAINER_NODE "pct exec $VMID -- timeout 10 curl -s http://127.0.0.1:4000/api/v2/status 2>&1" || echo "FAILED")
    fi
else
    API_RESPONSE=$(pct exec $VMID -- timeout 10 curl -s http://127.0.0.1:4000/api/v2/status 2>&1 || echo "FAILED")
fi

if echo "$API_RESPONSE" | grep -q "chain_id\|success"; then
    log_success "Blockscout is responding!"
    echo "$API_RESPONSE" | head -10
else
    log_warn "Blockscout may still be starting or has issues"
    echo "Response: $API_RESPONSE"
    echo ""
    log_info "Try checking logs:"
    echo "  pct exec $VMID -- journalctl -u blockscout -n 50"
    echo "  pct exec $VMID -- docker-compose -f /opt/blockscout/docker-compose.yml logs"
fi

echo ""
log_info "Script complete!"

