et -e

TARGET="/home/.../dashboard"
BRANCH="master"

while read oldrev newrev refname; do
  if [[ "$refname" = "refs/heads/$BRANCH" ]]; then
    echo "Deploying $BRANCH…"

    GIT_WORK_TREE="$TARGET" git checkout -f "$BRANCH"
    cd "$TARGET"

    composer install --no-interaction --prefer-dist --optimize-autoloader
    php artisan migrate --force

    # ─── FILAMENT OPTIMIZATION ─────────────────────────────────────
    # 1. Clear everything
    php artisan optimize:clear
    # 1a. (or specifically for Filament)
    php artisan filament:optimize-clear

    # 2. Rebuild
    php artisan optimize
    # 2a. (or specifically for Filament)
    php artisan filament:optimize
    # ────────────────────────────────────────────────────────────────

    # (Optional: additional cache commands)
    php artisan config:cache
    php artisan route:cache
    php artisan view:cache
    php artisan cache:clear

    sudo systemctl reload php8.1-fpm
    echo "✅ Deployment complete!"
  fi
done

