#!/bin/bash
# Deploy RPC Translator to a Proxmox VMID
# Usage: ./scripts/deploy-to-vmid.sh <VMID> <VMIP>

set -e

VMID="${1:-}"
VMIP="${2:-}"

if [ -z "$VMID" ] || [ -z "$VMIP" ]; then
  echo "Usage: $0 <VMID> <VMIP>"
  echo "Example: $0 2400 192.168.11.240"
  exit 1
fi

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
DEPLOY_DIR="/opt/rpc-translator-138"

echo "Deploying RPC Translator to VMID $VMID ($VMIP)..."

# Check if we can connect
echo "Checking connectivity to VMID $VMID..."
SSH_KEY=""
if [ -f ~/.ssh/proxmox_translator ]; then
  SSH_KEY="-i ~/.ssh/proxmox_translator"
fi
if ! ssh $SSH_KEY -o StrictHostKeyChecking=no -o ConnectTimeout=5 "root@$VMIP" "echo 'Connected'" 2>/dev/null; then
  echo "Error: Cannot connect to $VMIP. Please check SSH access."
  exit 1
fi

# Build locally
echo "Building TypeScript..."
cd "$PROJECT_DIR"
pnpm run build

# Install required dependencies on remote (rsync, nodejs, npm)
echo "Installing required dependencies on VMID $VMID..."
ssh $SSH_KEY -o StrictHostKeyChecking=no "root@$VMIP" "
  export DEBIAN_FRONTEND=noninteractive
  apt-get update -qq
  apt-get install -y -qq rsync curl >/dev/null 2>&1 || true
  # Check if Node.js is installed, if not install it
  if ! command -v node >/dev/null 2>&1; then
    curl -fsSL https://deb.nodesource.com/setup_20.x | bash - >/dev/null 2>&1
    apt-get install -y -qq nodejs >/dev/null 2>&1
  fi
  # Ensure npm is available
  if ! command -v npm >/dev/null 2>&1; then
    apt-get install -y -qq npm >/dev/null 2>&1
  fi
"

# Create deployment directory on remote
echo "Creating deployment directory on VMID $VMID..."
ssh $SSH_KEY -o StrictHostKeyChecking=no "root@$VMIP" "mkdir -p $DEPLOY_DIR"

# Copy files
echo "Copying files to VMID $VMID..."
RSYNC_SSH=""
if [ -n "$SSH_KEY" ]; then
  RSYNC_SSH="-e \"ssh $SSH_KEY -o StrictHostKeyChecking=no\""
fi
eval rsync -avz --delete $RSYNC_SSH \
  --exclude 'node_modules' \
  --exclude '.git' \
  --exclude 'dist' \
  --exclude '.env' \
  "$PROJECT_DIR/" "root@$VMIP:$DEPLOY_DIR/"

# Copy built files
eval rsync -avz $RSYNC_SSH "$PROJECT_DIR/dist/" "root@$VMIP:$DEPLOY_DIR/dist/"

# Copy package.json and install dependencies
echo "Installing dependencies on VMID $VMID..."
ssh $SSH_KEY -o StrictHostKeyChecking=no "root@$VMIP" "
  cd $DEPLOY_DIR
  # Use npm or pnpm if available
  if command -v pnpm >/dev/null 2>&1; then
    pnpm install --production
  else
    npm install --production
  fi
"

# Copy systemd service file
echo "Installing systemd service..."
ssh $SSH_KEY -o StrictHostKeyChecking=no "root@$VMIP" "mkdir -p /etc/systemd/system"
scp $SSH_KEY -o StrictHostKeyChecking=no "$PROJECT_DIR/systemd/rpc-translator-138.service" "root@$VMIP:/etc/systemd/system/"

# Note: .env file should be configured separately
echo ""
echo "✅ Deployment complete!"
echo ""
echo "Next steps on VMID $VMID:"
echo "1. Configure .env file:"
echo "   ssh root@$VMIP"
echo "   cd $DEPLOY_DIR"
echo "   cp env.template .env"
echo "   nano .env  # Edit with actual values"
echo ""
echo "2. Enable and start service:"
echo "   systemctl daemon-reload"
echo "   systemctl enable rpc-translator-138.service"
echo "   systemctl start rpc-translator-138.service"
echo "   systemctl status rpc-translator-138.service"
echo ""
