#!/usr/bin/env bash
# Installation script for Price Feed Keeper Service 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 '')"

KEEPER_USER="${KEEPER_USER:-keeper}"
KEEPER_GROUP="${KEEPER_GROUP:-keeper}"
KEEPER_HOME="/opt/keeper"
NODE_VERSION="${NODE_VERSION:-20}"

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 \
    curl \
    wget \
    jq \
    ca-certificates \
    gnupg

log_success "System packages updated"

# Install Node.js
log_info "Installing Node.js ${NODE_VERSION}..."
curl -fsSL https://deb.nodesource.com/setup_${NODE_VERSION}.x | bash -
apt-get install -y -qq nodejs

log_success "Node.js installed: $(node --version)"

log_info "Creating keeper user..."
if ! id -u "$KEEPER_USER" &>/dev/null; then
    useradd -r -s /bin/bash -d "$KEEPER_HOME" -m "$KEEPER_USER"
    log_success "Created keeper user"
else
    log_info "Keeper user already exists"
fi

# Create directories
log_info "Creating directories..."
mkdir -p "$KEEPER_HOME"
chown -R "$KEEPER_USER:$KEEPER_GROUP" "$KEEPER_HOME"

# Initialize npm project
log_info "Initializing Node.js project..."
cd "$KEEPER_HOME"
su - "$KEEPER_USER" -c "cd $KEEPER_HOME && npm init -y"
su - "$KEEPER_USER" -c "cd $KEEPER_HOME && npm install ethers@^6.0.0 dotenv@^16.0.0"

log_success "Node.js dependencies installed"

# Create configuration template
log_info "Creating configuration..."
cat > "$KEEPER_HOME/.env.template" <<'EOF'
# Price Feed Keeper Configuration
RPC_URL_138=http://192.168.11.250:8545
KEEPER_PRIVATE_KEY=
PRICE_FEED_KEEPER_ADDRESS=
UPDATE_INTERVAL=30

# Health check
HEALTH_PORT=3000
EOF

chown "$KEEPER_USER:$KEEPER_GROUP" "$KEEPER_HOME/.env.template"

# Create systemd service
log_info "Creating systemd service..."
cat > /etc/systemd/system/price-feed-keeper.service <<EOF
[Unit]
Description=Price Feed Keeper Service
After=network.target
Wants=network-online.target

[Service]
Type=simple
User=$KEEPER_USER
Group=$KEEPER_GROUP
WorkingDirectory=$KEEPER_HOME
Environment="PATH=/usr/bin:/usr/local/bin"
Environment="NODE_ENV=production"

EnvironmentFile=-$KEEPER_HOME/.env

ExecStart=/usr/bin/node $KEEPER_HOME/keeper-service.js

Restart=always
RestartSec=10

NoNewPrivileges=true
PrivateTmp=true

StandardOutput=journal
StandardError=journal
SyslogIdentifier=price-feed-keeper

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload

log_success "Keeper installation completed!"
log_info "Next steps:"
log_info "1. Copy keeper-service.js to $KEEPER_HOME/"
log_info "2. Configure $KEEPER_HOME/.env"
log_info "3. Start service: systemctl start price-feed-keeper"

