#!/usr/bin/env bash
# Installation script for Hyperledger Indy in LXC container
# Indy is a distributed ledger for self-sovereign identity

set -euo pipefail

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

INDY_VERSION="${INDY_VERSION:-1.18.1}"
INDY_USER="${INDY_USER:-indy}"
INDY_GROUP="${INDY_GROUP:-indy}"
INDY_HOME="/opt/indy"
INDY_DATA="/var/lib/indy"

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 \
    ca-certificates \
    gnupg \
    lsb-release \
    python3 \
    python3-pip \
    python3-dev \
    libssl-dev \
    libffi-dev \
    build-essential \
    pkg-config \
    libzmq5 \
    libzmq3-dev

log_success "System packages updated"

# Start Docker
systemctl enable docker
systemctl start docker

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

# Install Python dependencies
log_info "Installing Python dependencies..."
pip3 install --break-system-packages -q \
    python3-indy \
    indy-plenum \
    indy-node \
    aiohttp

log_success "Python dependencies installed"

# Create directories
log_info "Creating directories..."
mkdir -p "$INDY_HOME" "$INDY_DATA" "$INDY_DATA/data" "$INDY_DATA/logs" "$INDY_DATA/keys"
chown -R "$INDY_USER:$INDY_GROUP" "$INDY_HOME" "$INDY_DATA"

# Download Indy pool configuration
log_info "Setting up Indy configuration..."
cd "$INDY_HOME"

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

services:
  indy-node-1:
    image: indy-node:${INDY_VERSION:-1.18.1}
    container_name: indy-node-1
    environment:
      - NODE_NAME=Node1
      - NODE_IP=0.0.0.0
      - NODE_PORT=9701
      - CLIENT_IP=0.0.0.0
      - CLIENT_PORT=9702
      - NETWORK_NAME=${NETWORK_NAME:-sandbox}
    volumes:
      - indy-data:/var/lib/indy
      - indy-logs:/var/log/indy
    ports:
      - "9701:9701"
      - "9702:9702"
    restart: unless-stopped
    networks:
      - indy-network

  indy-node-2:
    image: indy-node:${INDY_VERSION:-1.18.1}
    container_name: indy-node-2
    environment:
      - NODE_NAME=Node2
      - NODE_IP=0.0.0.0
      - NODE_PORT=9703
      - CLIENT_IP=0.0.0.0
      - CLIENT_PORT=9704
      - NETWORK_NAME=${NETWORK_NAME:-sandbox}
    volumes:
      - indy-data:/var/lib/indy
      - indy-logs:/var/log/indy
    ports:
      - "9703:9703"
      - "9704:9704"
    restart: unless-stopped
    networks:
      - indy-network

  indy-node-3:
    image: indy-node:${INDY_VERSION:-1.18.1}
    container_name: indy-node-3
    environment:
      - NODE_NAME=Node3
      - NODE_IP=0.0.0.0
      - NODE_PORT=9705
      - CLIENT_IP=0.0.0.0
      - CLIENT_PORT=9706
      - NETWORK_NAME=${NETWORK_NAME:-sandbox}
    volumes:
      - indy-data:/var/lib/indy
      - indy-logs:/var/log/indy
    ports:
      - "9705:9705"
      - "9706:9706"
    restart: unless-stopped
    networks:
      - indy-network

  indy-node-4:
    image: indy-node:${INDY_VERSION:-1.18.1}
    container_name: indy-node-4
    environment:
      - NODE_NAME=Node4
      - NODE_IP=0.0.0.0
      - NODE_PORT=9707
      - CLIENT_IP=0.0.0.0
      - CLIENT_PORT=9708
      - NETWORK_NAME=${NETWORK_NAME:-sandbox}
    volumes:
      - indy-data:/var/lib/indy
      - indy-logs:/var/log/indy
    ports:
      - "9707:9707"
      - "9708:9708"
    restart: unless-stopped
    networks:
      - indy-network

volumes:
  indy-data:
  indy-logs:

networks:
  indy-network:
    driver: bridge
EOF

chown -R "$INDY_USER:$INDY_GROUP" "$INDY_HOME"

log_success "Indy configuration created"

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

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

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable indy.service

log_success "Systemd service created"

log_success "Indy installation completed!"
log_info "Next steps:"
log_info "1. Edit $INDY_HOME/docker-compose.yml to configure network name and nodes"
log_info "2. Generate genesis transactions for the pool"
log_info "3. Configure node keys and certificates"
log_info "4. Start service: systemctl start indy"
log_info "5. Indy pool will run 4 nodes for consensus"

