#!/usr/bin/env bash
# --------------------------------------------------------------------------
# Niyarasa — deployment script
# --------------------------------------------------------------------------
# Run from the project root on the production server after a `git pull`
# (or as the last step of a CI/CD pipeline). Idempotent — safe to re-run.
#
# Usage: bash deploy/deploy.sh

set -euo pipefail

echo "==> Putting the site into maintenance mode"
php artisan down --retry=60 || true

echo "==> Installing PHP dependencies (no dev, optimized autoloader)"
composer install --no-dev --optimize-autoloader --no-interaction

echo "==> Installing and building frontend assets"
npm ci
npm run build

echo "==> Running database migrations"
php artisan migrate --force

echo "==> Linking public storage (safe to re-run — no-ops if it already exists)"
php artisan storage:link || true

echo "==> Rebuilding framework caches"
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache

echo "==> Restarting queued job workers so they pick up the new code"
php artisan queue:restart

echo "==> Bringing the site back up"
php artisan up

echo "==> Deployment complete"
