#!/bin/bash
# Deployment script for Project Progress Tracker to ppt24.com

# Set variables
SERVER="root@172.105.78.162"
SERVER_DIR="/var/www/ppt24.com"
LOCAL_DIR=$(pwd)
DB_FILENAME="project_tracker.sqlite"

set -e # Exit on error

echo "=== DEPLOYMENT STARTED ==="
echo "Building React frontend for production..."
cd $LOCAL_DIR/frontend
npm run build

echo "Creating deployment package..."
cd $LOCAL_DIR
rm -rf deployment
mkdir -p deployment

# Copy frontend build
cp -r frontend/build deployment/

# Copy backend code
cp -r backend deployment/

# Use production environment file
cp backend/.env.production deployment/backend/.env

# Prepare database directory
mkdir -p deployment/database

# Check if we have a database file to copy
if [ -f "backend/data/$DB_FILENAME" ]; then
  echo "Copying existing SQLite database..."
  cp "backend/data/$DB_FILENAME" "deployment/database/$DB_FILENAME"
else
  echo "No existing database found. A new one will be created on the server."
fi

echo "Deploying to server at $SERVER:$SERVER_DIR"
ssh $SERVER "mkdir -p $SERVER_DIR/frontend"
ssh $SERVER "mkdir -p $SERVER_DIR/backend"
ssh $SERVER "mkdir -p $SERVER_DIR/database"

# Transfer files
echo "Transferring frontend files..."
scp -r deployment/build/* $SERVER:$SERVER_DIR/frontend/

echo "Transferring backend files..."
scp -r deployment/backend/* $SERVER:$SERVER_DIR/backend/

echo "Transferring database..."
if [ -f "deployment/database/$DB_FILENAME" ]; then
  scp "deployment/database/$DB_FILENAME" "$SERVER:$SERVER_DIR/database/$DB_FILENAME"
fi

# Setup backend on server
echo "Setting up backend on server..."
ssh $SERVER "cd $SERVER_DIR/backend && npm install --only=production"

# Create PM2 configuration file
echo "Creating PM2 configuration..."
cat > deployment/ecosystem.config.js << EOL
module.exports = {
  apps: [
    {
      name: 'ppt24-backend',
      script: '$SERVER_DIR/backend/src/server.js',
      env: {
        NODE_ENV: 'production',
        PORT: 5000,
        DB_PATH: '$SERVER_DIR/database/$DB_FILENAME',
        JWT_SECRET: '$(openssl rand -hex 32)',
        CORS_ORIGIN: 'https://ppt24.com',
      },
      instances: 1,
      exec_mode: 'fork',
      watch: false,
      max_memory_restart: '300M',
    }
  ]
};
EOL

# Deploy PM2 configuration
scp deployment/ecosystem.config.js $SERVER:$SERVER_DIR/

# Start the application with PM2
echo "Starting application with PM2..."
ssh $SERVER "cd $SERVER_DIR && pm2 delete ppt24-backend 2>/dev/null || true"
ssh $SERVER "cd $SERVER_DIR && pm2 start ecosystem.config.js && pm2 save && pm2 startup && pm2 save"

# Set up certbot for HTTPS
echo "Setting up HTTPS with Let's Encrypt..."
ssh $SERVER "apt install -y certbot python3-certbot-nginx"
ssh $SERVER "certbot --nginx -d ppt24.com -d www.ppt24.com --non-interactive --agree-tos --email admin@ppt24.com || echo 'Certificate setup can be completed later'"

echo "=== DEPLOYMENT COMPLETE ==="
echo "The application should now be accessible at https://ppt24.com"
echo "You may need to update your DNS settings at Cloudflare to point to your Linode server IP: 172.105.78.162"
