#!/bin/bash
set -e

# Load environment variables
if [ -f .env ]; then
  set -a
  source .env
  set +a
fi

if [ -z "$VPS_HOST" ] || [ -z "$VPS_USER" ]; then
  echo "Error: VPS_HOST and VPS_USER must be set in .env"
  echo "Please add them to your .env file:"
  echo "VPS_USER=your-username"
  echo "VPS_HOST=your-ip-address"
  exit 1
fi

# Default remote directory to ~/Aurora if not specified
REMOTE_DIR="${VPS_PROJECT_PATH:-~/Aurora}"

echo -e "\033[1;33m🚀 Deploying to $VPS_USER@$VPS_HOST:$REMOTE_DIR...\033[0m"

# Execute commands on remote server
ssh -t "$VPS_USER@$VPS_HOST" "cd $REMOTE_DIR && \
echo '⬇️  Pulling latest changes...' && \
git pull && \
echo '🏗️  Building production containers...' && \
docker compose -f docker-compose.prod.yml build && \
echo '🚀 Starting services...' && \
docker compose -f docker-compose.prod.yml up -d && \
echo '✅ Deployment complete!'"
