#!/bin/bash
# ============================================================
#  Svishtov Live – Backend Server Install Script
#  Run this ONCE on the CentOS 7 VPS after uploading files.
# ============================================================
set -e

echo "=== [1/7] Installing Composer dependencies ==="
composer install --no-dev --optimize-autoloader --ignore-platform-req=ext-pcntl

echo "=== [2/7] Configuring .env ==="
if [ ! -f .env ]; then
  cp .env.example .env
  echo "⚠  Created .env from .env.example — please fill in DB_PASSWORD before continuing!"
  exit 1
fi

echo "=== [3/7] Generating application key (if not set) ==="
php artisan key:generate --no-interaction

echo "=== [4/7] Running migrations ==="
php artisan migrate --force

echo "=== [5/7] Seeding demo data ==="
php artisan db:seed --force

echo "=== [6/7] Publishing vendor assets ==="
php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider" --force

echo "=== [7/7] Optimizing for production ==="
php artisan optimize
php artisan storage:link

echo "=== Setting permissions ==="
chmod -R 775 storage bootstrap/cache
chown -R nginx:nginx storage bootstrap/cache

echo ""
echo "✅  Backend install complete!"
echo "   Next: configure Supervisor for Reverb + Queue (see 04-SUPERVISOR-CONFIG.md)"
