#!/bin/bash
# ═══════════════════════════════════════════════════════════════
# Rapha-Bethel BNPS — Server Deployment Script
# Run this on the InMotion server via SSH:
#   chmod +x deploy.sh && bash deploy.sh
# ═══════════════════════════════════════════════════════════════
set -e   # stop on any error

DEPLOY_DIR="/home/dnjill5/raphabethel.org/portal"
VENV_DIR="/home/dnjill5/virtualenv/raphabethel.org/portal/3.13"
PYTHON="$VENV_DIR/bin/python3"
PIP="$VENV_DIR/bin/pip"

echo ""
echo "═══════════════════════════════════════════"
echo "  Rapha-Bethel BNPS — Deployment"
echo "═══════════════════════════════════════════"
echo ""

cd "$DEPLOY_DIR"

# ── Step 1: Install / upgrade packages ──────────────────────────
echo "▶ Installing Python packages..."
$PIP install --upgrade pip
$PIP install -r requirements.txt
echo "✓ Packages installed"

# ── Step 2: Collect static files ────────────────────────────────
echo "▶ Collecting static files..."
$PYTHON manage.py collectstatic --noinput --settings=backend.settings_production
echo "✓ Static files collected"

# ── Step 3: Run migrations ───────────────────────────────────────
echo "▶ Running database migrations..."
$PYTHON manage.py migrate --settings=backend.settings_production
echo "✓ Migrations complete"

# ── Step 4: Seed default data ────────────────────────────────────
echo "▶ Seeding school data (safe — skips existing records)..."
$PYTHON scripts/seed_school_data.py || echo "⚠ Seed skipped or already done"
echo "✓ Seed complete"

# ── Step 5: Restart Passenger ────────────────────────────────────
echo "▶ Restarting application server..."
mkdir -p tmp && touch tmp/restart.txt
echo "✓ Passenger restart triggered"

echo ""
echo "═══════════════════════════════════════════"
echo "  ✓ Deployment complete!"
echo "  Visit: https://raphabethel.org/portal"
echo "═══════════════════════════════════════════"
echo ""