#!/usr/bin/env bash
set -euo pipefail

# Jalankan di server hosting (SSH), dari root folder project Laravel.
# Contoh: bash deploy/deploy.sh

echo "==> Composer (production)"
composer install --no-dev --optimize-autoloader --no-interaction

echo "==> Frontend assets"
if command -v npm >/dev/null 2>&1; then
  npm ci --ignore-scripts 2>/dev/null || npm install --ignore-scripts
  npm run build
else
  echo "    npm tidak ditemukan — pastikan folder public/build sudah ter-upload dari lokal."
fi

echo "==> Storage link"
php artisan storage:link --force 2>/dev/null || true

echo "==> Migration"
php artisan migrate --force

echo "==> Cache optimasi"
php artisan config:cache
php artisan route:cache
php artisan view:cache

echo "==> Permission (Linux)"
chmod -R ug+rwx storage bootstrap/cache 2>/dev/null || true

echo "Deploy selesai. Cek https://okelaundry.my.id/up"
