#!/bin/bash
# ============================================================
# CarWash POS — cPanel Deployment Script
# Run from your app root: bash deploy/deploy.sh
# ============================================================

set -e

echo "🚀 CarWash POS — Deploying..."

# 1. Put app in maintenance mode
php artisan down --render="errors.503" --retry=60

# 2. Install/update dependencies (no dev packages)
composer install --no-dev --optimize-autoloader --no-interaction

# 3. Run migrations
php artisan migrate --force

# 4. Clear all caches
php artisan config:clear
php artisan route:clear
php artisan view:clear
php artisan cache:clear
php artisan event:clear

# 5. Rebuild caches for production speed
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache

# 6. Ensure storage link exists
php artisan storage:link 2>/dev/null || true

# 7. Set correct permissions
chmod -R 755 storage bootstrap/cache
find storage -type f -exec chmod 644 {} \;

# 8. Bring app back online
php artisan up

echo "✅ Deployment complete!"
