#!/bin/bash

# =============================================================================
# Deploy Production Script for Moro Staging
# =============================================================================

set -e  # Dừng script nếu có lỗi

PROJECT_DIR="/var/www/html/moro-staging"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)

echo "=========================================="
echo "Starting deployment at $(date)"
echo "=========================================="

cd $PROJECT_DIR

# -----------------------------------------------------------------------------
# 1. Backup uncommitted changes (thay vì mất khi stash)
# -----------------------------------------------------------------------------
echo "Checking for uncommitted changes..."
if [[ -n $(git status --porcelain) ]]; then
    echo "Found uncommitted changes, creating backup..."
    git stash save "auto-backup-before-deploy-$TIMESTAMP"
    echo "Changes backed up to stash: auto-backup-before-deploy-$TIMESTAMP"
    echo "To restore: git stash list && git stash apply stash@{0}"
else
    echo "No uncommitted changes found."
fi

# -----------------------------------------------------------------------------
# 2. Pull latest code
# -----------------------------------------------------------------------------
echo "Pulling latest code from develop..."
git pull origin develop

# -----------------------------------------------------------------------------
# 3. Set proper permissions (755 for directories, 644 for files)
# -----------------------------------------------------------------------------
echo "Setting proper permissions..."
chmod -R 775 storage/
chmod -R 775 bootstrap/cache/

# -----------------------------------------------------------------------------
# 4. Install dependencies & optimize
# -----------------------------------------------------------------------------
echo "Running composer dump-autoload..."
composer dump-autoload --optimize

echo "Running migrations..."
php artisan migrate --force

echo "Clearing and rebuilding caches..."
php artisan config:clear
php artisan cache:clear
php artisan view:cache
php artisan route:cache

# -----------------------------------------------------------------------------
# 5. Build frontend assets
# -----------------------------------------------------------------------------
echo "Building frontend assets..."
npm run production-all

# -----------------------------------------------------------------------------
# 6. Restart queue workers (QUAN TRỌNG khi sửa Mail classes)
# -----------------------------------------------------------------------------
echo "Restarting queue workers..."
supervisorctl restart moro-dev-queue:*
echo "Queue workers restarted successfully!"

# -----------------------------------------------------------------------------
# Done
# -----------------------------------------------------------------------------
echo "=========================================="
echo "Deployment completed at $(date)"
echo "=========================================="

