#!/bin/bash

# Atlas Deployment Script
# Deploys Atlas to adampowell.pro/atlas

set -e

SERVER="root@198.211.114.12"
REMOTE_PATH="/var/www/adampowell.pro/Atlas"
SSH_KEY="$HOME/.ssh/id_ed25519"

echo "🚀 Deploying Atlas to adampowell.pro..."
echo ""

# Step 1: Upload files
echo "📤 Uploading files to server..."
rsync -avz --exclude 'node_modules' --exclude '.env' --exclude 'data' \
  -e "ssh -i $SSH_KEY" \
  ./ $SERVER:$REMOTE_PATH/

# Step 2: Install dependencies and restart
echo "📦 Installing dependencies..."
ssh -i "$SSH_KEY" $SERVER << 'EOF'
  cd /var/www/adampowell.pro/Atlas/broker

  # Install dependencies
  npm install --production

  # Restart service
  echo "♻️ Restarting Atlas service..."
  systemctl restart atlas

  # Check status
  echo ""
  echo "✅ Deployment complete!"
  echo ""
  echo "Service status:"
  systemctl status atlas --no-pager -l

  echo ""
  echo "Recent logs:"
  journalctl -u atlas -n 20 --no-pager
EOF

echo ""
echo "🎉 Atlas deployed successfully!"
echo "📱 Access at: https://adampowell.pro/atlas"
echo ""
