#!/usr/bin/env bash
# Installation script for Blockscout Explorer in LXC container

set -euo pipefail

source /dev/stdin <<<"$(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/build.func 2>/dev/null || echo '')"

BLOCKSCOUT_USER="${BLOCKSCOUT_USER:-blockscout}"
BLOCKSCOUT_GROUP="${BLOCKSCOUT_GROUP:-blockscout}"
BLOCKSCOUT_HOME="/opt/blockscout"

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}[WARNING]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

log_info "Updating system packages..."
export LC_ALL=C
export LANG=C
export DEBIAN_FRONTEND=noninteractive
apt-get update -qq
apt-get upgrade -y -qq
apt-get install -y -qq \
    docker.io \
    docker-compose \
    postgresql-client \
    curl \
    wget \
    jq \
    ca-certificates \
    gnupg

log_success "System packages updated"

# Start Docker
systemctl enable docker
systemctl start docker

log_info "Creating blockscout user..."
if ! id -u "$BLOCKSCOUT_USER" &>/dev/null; then
    useradd -r -s /bin/bash -d "$BLOCKSCOUT_HOME" -m "$BLOCKSCOUT_USER"
    usermod -aG docker "$BLOCKSCOUT_USER"
    log_success "Created blockscout user"
else
    log_info "Blockscout user already exists"
    usermod -aG docker "$BLOCKSCOUT_USER"
fi

# Create directories
log_info "Creating directories..."
mkdir -p "$BLOCKSCOUT_HOME"
chown -R "$BLOCKSCOUT_USER:$BLOCKSCOUT_GROUP" "$BLOCKSCOUT_HOME"

# Create docker-compose.yml
log_info "Setting up Blockscout..."
cd "$BLOCKSCOUT_HOME"

cat > docker-compose.yml <<'EOF'
version: '3.8'

services:
  postgres:
    image: postgres:15-alpine
    container_name: blockscout-postgres
    environment:
      POSTGRES_USER: blockscout
      POSTGRES_PASSWORD: ${DB_PASSWORD:-blockscout}
      POSTGRES_DB: blockscout
    volumes:
      - postgres-data:/var/lib/postgresql/data
    restart: unless-stopped
    networks:
      - blockscout-network

  blockscout:
    image: blockscout/blockscout:latest
    container_name: blockscout
    depends_on:
      - postgres
    environment:
      - DATABASE_URL=postgresql://blockscout:${DB_PASSWORD:-blockscout}@postgres:5432/blockscout
      - ETHEREUM_JSONRPC_HTTP_URL=${RPC_URL:-http://192.168.11.250:8545}
      - ETHEREUM_JSONRPC_WS_URL=${WS_URL:-ws://192.168.11.250:8546}
      - ETHEREUM_JSONRPC_TRACE_URL=${RPC_URL:-http://192.168.11.250:8545}
      - ETHEREUM_JSONRPC_VARIANT=besu
      - CHAIN_ID=${CHAIN_ID:-138}
      - COIN=ETH
      - BLOCKSCOUT_HOST=${BLOCKSCOUT_HOST:-localhost}
      - BLOCKSCOUT_PROTOCOL=http
      - SECRET_KEY_BASE=${SECRET_KEY:-$(openssl rand -hex 64)}
    ports:
      - "4000:4000"
    volumes:
      - blockscout-data:/app/apps/explorer/priv/static
    restart: unless-stopped
    networks:
      - blockscout-network

volumes:
  postgres-data:
  blockscout-data:

networks:
  blockscout-network:
    driver: bridge
EOF

chown -R "$BLOCKSCOUT_USER:$BLOCKSCOUT_GROUP" "$BLOCKSCOUT_HOME"

# Create systemd service
log_info "Creating systemd service..."
cat > /etc/systemd/system/blockscout.service <<EOF
[Unit]
Description=Blockscout Explorer
After=docker.service postgresql.service
Requires=docker.service

[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=$BLOCKSCOUT_HOME
User=$BLOCKSCOUT_USER
Group=$BLOCKSCOUT_GROUP
ExecStart=/usr/bin/docker-compose -f $BLOCKSCOUT_HOME/docker-compose.yml up -d
ExecStop=/usr/bin/docker-compose -f $BLOCKSCOUT_HOME/docker-compose.yml down
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable blockscout.service

log_success "Blockscout installation completed!"
log_info "Next steps:"
log_info "1. Edit $BLOCKSCOUT_HOME/docker-compose.yml to set RPC_URL and other configs"
log_info "2. Start service: systemctl start blockscout"
log_info "3. Blockscout will be available at: http://localhost:4000"

