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

TOKEN_USER="${TOKEN_USER:-tokenization}"
TOKEN_GROUP="${TOKEN_GROUP:-tokenization}"
TOKEN_HOME="/opt/financial-tokenization"
TOKEN_DATA="/var/lib/financial-tokenization"

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 tokenization user..."
if ! id -u "$TOKEN_USER" &>/dev/null; then
    useradd -r -s /bin/bash -d "$TOKEN_HOME" -m "$TOKEN_USER"
    log_success "Created tokenization user"
else
    log_info "Tokenization user already exists"
fi

# Create directories
log_info "Creating directories..."
mkdir -p "$TOKEN_HOME" "$TOKEN_DATA"
chown -R "$TOKEN_USER:$TOKEN_GROUP" "$TOKEN_HOME" "$TOKEN_DATA"

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

# Install dependencies
log_info "Installing Python dependencies..."
su - "$TOKEN_USER" -c "cd $TOKEN_HOME && source venv/bin/activate && \
    pip install --upgrade pip && \
    pip install flask flask-restx web3 requests structlog python-dotenv"

log_success "Python dependencies installed"

# Create configuration template
log_info "Creating configuration..."
cat > "$TOKEN_HOME/.env.template" <<'EOF'
# Financial Tokenization Configuration
FIREFLY_API_URL=http://192.168.11.66:5000
FIREFLY_API_KEY=
BESU_RPC_URL=http://192.168.11.250:8545
CHAIN_ID=138

# Flask
FLASK_ENV=production
FLASK_PORT=5001
EOF

chown "$TOKEN_USER:$TOKEN_GROUP" "$TOKEN_HOME/.env.template"

# Create systemd service
log_info "Creating systemd service..."
cat > /etc/systemd/system/financial-tokenization.service <<EOF
[Unit]
Description=Financial Tokenization Service
After=network.target
Wants=network-online.target

[Service]
Type=simple
User=$TOKEN_USER
Group=$TOKEN_GROUP
WorkingDirectory=$TOKEN_HOME
Environment="PATH=$TOKEN_HOME/venv/bin:/usr/local/bin:/usr/bin:/bin"

EnvironmentFile=-$TOKEN_HOME/.env

ExecStart=$TOKEN_HOME/venv/bin/python $TOKEN_HOME/financial_tokenization_service.py

Restart=always
RestartSec=10

NoNewPrivileges=true
PrivateTmp=true

StandardOutput=journal
StandardError=journal
SyslogIdentifier=financial-tokenization

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload

log_success "Financial Tokenization installation completed!"

