#!/usr/bin/env bash
# One-shot builder: turns this generated source into a complete, ready-to-zip Laravel
# project with vendor/ included. Run this ONCE on any machine with internet + Composer
# + PHP 8.2+ (your laptop, a cheap VPS, GitHub Codespaces, etc.) — it does NOT need to be
# the cPanel server itself. The output folder is what you upload to cPanel.
#
# Usage:
#   ./build.sh                     # builds ./school-app next to this script
#   ./build.sh /path/to/output     # builds at a custom path
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
OUT="${1:-$HERE/../school-app}"

command -v composer >/dev/null || { echo "Composer is not installed. Install it from https://getcomposer.org and re-run."; exit 1; }
command -v php >/dev/null || { echo "PHP is not installed or not on PATH."; exit 1; }
PHP_VER=$(php -r 'echo PHP_VERSION;')
echo "Using PHP $PHP_VER"

echo "==> Creating fresh Laravel skeleton at $OUT"
composer create-project laravel/laravel "$OUT" "^11.0" --no-interaction --prefer-dist

cd "$OUT"
echo "==> Installing spatie/laravel-permission"
composer require spatie/laravel-permission --no-interaction

echo "==> Merging generated app code"
cp -rf "$HERE/app/Models/." app/Models/
cp -rf "$HERE/app/Http/Controllers/." app/Http/Controllers/
mkdir -p app/Services app/Jobs
cp -rf "$HERE/app/Services/." app/Services/
cp -rf "$HERE/app/Jobs/." app/Jobs/
cp -rf "$HERE/app/Console/Commands/." app/Console/Commands/
cp -f "$HERE/app/Providers/AppServiceProvider.php" app/Providers/AppServiceProvider.php
cp -f "$HERE/config/school.php" "$HERE/config/school_resources.php" config/
cp -rf "$HERE/database/migrations/." database/migrations/
cp -rf "$HERE/database/seeders/." database/seeders/
mkdir -p resources/views/school
cp -rf "$HERE/resources/views/school/." resources/views/school/
cp -f "$HERE/routes/web.php" routes/web.php
cp -f "$HERE/routes/console.php" routes/console.php

echo "==> Publishing spatie/laravel-permission config + migration"
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --no-interaction

echo "==> Writing .env (edit DB_* and mail/SMS/Stripe values before deploying)"
cp .env.example .env 2>/dev/null || true
if [ -f "$HERE/.env.example" ]; then
  # carry over the school-specific keys (SCHOOL_CURRENCY, SMS_*, WHATSAPP_*, STRIPE_*) that
  # a bare Laravel .env.example doesn't have
  cat "$HERE/.env.example" | grep -E '^(SCHOOL_|SMS_|WHATSAPP_|FCM_|STRIPE_)' >> .env
fi
php artisan key:generate --force

echo "==> Placing the web installer (public/install.php)"
cp -f "$HERE/_installer/install.php" public/install.php

chmod -R 755 storage bootstrap/cache

echo
echo "=================================================================="
echo " Build complete: $OUT"
echo "=================================================================="
echo "Next:"
echo "  1. Zip this whole folder (composer.lock, vendor/, everything) and"
echo "     upload it to cPanel, OUTSIDE public_html (see README-CPANEL.md)."
echo "  2. Point the domain's document root at <project>/public."
echo "  3. Visit https://yourdomain.com/install.php and complete the form."
echo "     It sets up the database, runs migrations+seed, creates your"
echo "     admin login, and deletes itself when done."
echo "=================================================================="
