#!/bin/bash

echo "🚀 Installing Node.js 20 in current folder..."

# 1. Download Node.js 20 (Linux x64, tar.gz version since xz may not work on cPanel)
curl -O https://nodejs.org/dist/v20.11.1/node-v20.11.1-linux-x64.tar.gz

# 2. Extract
tar -xzf node-v20.11.1-linux-x64.tar.gz

# 3. Rename folder to nodejs for simplicity
mv node-v20.11.1-linux-x64 nodejs

# 4. Add Node.js binaries to PATH (for current session)
export PATH=$PWD/nodejs/bin:$PATH

# 5. Show installed versions
echo "✅ Node version: $(node -v)"
echo "✅ NPM version: $(npm -v)"

# 6. Install dependencies & build
echo "📦 Installing npm dependencies..."
npm install

echo "🎨 Building production assets..."
npm run build

echo "🧹 Clearing Laravel caches..."
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear

echo "✅ Done! Built assets should now be in public/build/"
