#!/usr/bin/env bash
# Installation script for Hyperledger Cacti in LXC container
# Cacti is a blockchain integration platform

set -euo pipefail

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

CACTI_VERSION="${CACTI_VERSION:-latest}"
CACTI_USER="${CACTI_USER:-cacti}"
CACTI_GROUP="${CACTI_GROUP:-cacti}"
CACTI_HOME="/opt/cacti"
CACTI_DATA="/var/lib/cacti"

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 \
    curl \
    wget \
    jq \
    nodejs \
    npm \
    ca-certificates \
    gnupg \
    lsb-release

log_success "System packages updated"

# Start Docker
systemctl enable docker
systemctl start docker

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

# Create directories
log_info "Creating directories..."
mkdir -p "$CACTI_HOME" "$CACTI_DATA"
chown -R "$CACTI_USER:$CACTI_GROUP" "$CACTI_HOME" "$CACTI_DATA"

# Download Cacti docker-compose
log_info "Downloading Cacti configuration..."
cd "$CACTI_HOME"

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

services:
  cactus-api:
    image: ghcr.io/hyperledger/cactus-cmd-api-server:${CACTI_VERSION:-latest}
    container_name: cactus-api
    ports:
      - "4000:4000"
      - "4001:4001"
    environment:
      - CACTUS_NODE_ID=cactus-node-1
      - CACTUS_LOG_LEVEL=info
    volumes:
      - cacti-data:/var/lib/cactus
    restart: unless-stopped
    networks:
      - cacti-network

  besu-connector:
    image: ghcr.io/hyperledger/cactus-plugin-ledger-connector-besu:${CACTI_VERSION:-latest}
    container_name: cactus-besu-connector
    depends_on:
      - cactus-api
    environment:
      - BESU_RPC_URL=${BESU_RPC_URL:-http://192.168.11.250:8545}
      - BESU_WS_URL=${BESU_WS_URL:-ws://192.168.11.250:8546}
      - CHAIN_ID=${CHAIN_ID:-138}
      - CACTUS_NODE_ID=cactus-node-1
    ports:
      - "4100:4100"
    restart: unless-stopped
    networks:
      - cacti-network

volumes:
  cacti-data:

networks:
  cacti-network:
    driver: bridge
EOF

chown -R "$CACTI_USER:$CACTI_GROUP" "$CACTI_HOME"

log_success "Cacti configuration created"

# Create systemd service
log_info "Creating systemd service..."
cat > /etc/systemd/system/cacti.service <<EOF
[Unit]
Description=Hyperledger Cacti
After=docker.service
Requires=docker.service

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

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable cacti.service

log_success "Systemd service created"

log_success "Cacti installation completed!"
log_info "Next steps:"
log_info "1. Edit $CACTI_HOME/docker-compose.yml to set BESU_RPC_URL and other configs"
log_info "2. Set environment variables in /etc/systemd/system/cacti.service or .env file"
log_info "3. Start service: systemctl start cacti"
log_info "4. Cacti API will be available at: http://localhost:4000"

