#!/bin/bash

# تنظیم مجوزها برای storage و bootstrap/cache
echo "Fixing permissions for storage and bootstrap/cache..."
chown -R www-data:www-data storage bootstrap/cache
chmod -R 775 storage bootstrap/cache
find storage bootstrap/cache -type d -exec chmod 775 {} \;
find storage bootstrap/cache -type f -exec chmod 664 {} \;

# تولید APP_KEY اگر وجود نداشته باشد
if ! grep -q "^APP_KEY=base64:" .env; then
  echo "Generating APP_KEY..."
  php artisan key:generate
fi

# اجرای migrations
echo "🧩 Running migrations..."
php artisan migrate --force

# اجرای Vite در حالت توسعه
echo "⚙️ Starting Vite development server..."
npm run dev &

# اجرای PHP-FPM
exec "$@"