#!/usr/bin/env bash
# Installation script for CCIP Monitor 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 '')"

CCIP_USER="${CCIP_USER:-ccip}"
CCIP_GROUP="${CCIP_GROUP:-ccip}"
CCIP_HOME="/opt/ccip-monitor"
CCIP_DATA="/var/lib/ccip-monitor"

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 \
    python3 \
    python3-pip \
    python3-venv \
    curl \
    wget \
    jq \
    ca-certificates

log_success "System packages updated"

log_info "Creating ccip user..."
if ! id -u "$CCIP_USER" &>/dev/null; then
    useradd -r -s /bin/bash -d "$CCIP_HOME" -m "$CCIP_USER"
    log_success "Created ccip user"
else
    log_info "CCIP user already exists"
fi

# Create directories
log_info "Creating directories..."
mkdir -p "$CCIP_HOME" "$CCIP_DATA"
chown -R "$CCIP_USER:$CCIP_GROUP" "$CCIP_HOME" "$CCIP_DATA"

# Create Python virtual environment
log_info "Setting up Python environment..."
cd "$CCIP_HOME"
su - "$CCIP_USER" -c "python3 -m venv venv"

# Install dependencies
log_info "Installing Python dependencies..."
su - "$CCIP_USER" -c "cd $CCIP_HOME && source venv/bin/activate && \
    pip install --upgrade pip && \
    pip install web3 prometheus-client requests opentelemetry-api \
    opentelemetry-sdk opentelemetry-exporter-otlp-proto-grpc \
    opentelemetry-instrumentation-requests opentelemetry-instrumentation-web3"

log_success "Python dependencies installed"

# Create configuration template
log_info "Creating configuration..."
cat > "$CCIP_HOME/.env.template" <<'EOF'
# CCIP Monitor Configuration
RPC_URL_138=http://192.168.11.250:8545
CCIP_ROUTER_ADDRESS=
CCIP_SENDER_ADDRESS=
LINK_TOKEN_ADDRESS=

# Monitoring
METRICS_PORT=8000
CHECK_INTERVAL=60
ALERT_WEBHOOK=

# OpenTelemetry (optional)
OTEL_ENABLED=false
OTEL_ENDPOINT=http://localhost:4317
EOF

chown "$CCIP_USER:$CCIP_GROUP" "$CCIP_HOME/.env.template"

# Create systemd service
log_info "Creating systemd service..."
cat > /etc/systemd/system/ccip-monitor.service <<EOF
[Unit]
Description=CCIP Monitor Service
After=network.target
Wants=network-online.target

[Service]
Type=simple
User=$CCIP_USER
Group=$CCIP_GROUP
WorkingDirectory=$CCIP_HOME
Environment="PATH=$CCIP_HOME/venv/bin:/usr/local/bin:/usr/bin:/bin"

EnvironmentFile=-$CCIP_HOME/.env

ExecStart=$CCIP_HOME/venv/bin/python $CCIP_HOME/ccip_monitor.py

Restart=always
RestartSec=10

NoNewPrivileges=true
PrivateTmp=true

StandardOutput=journal
StandardError=journal
SyslogIdentifier=ccip-monitor

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload

log_success "CCIP Monitor installation completed!"

