#!/bin/bash
# Run this from the cPanel Terminal after cloning the repo
# Usage: cd ~/api.isoftke.co.ke/backend && bash deploy.sh

set -e

echo "=== FitMtaani Backend Deployment ==="

# Copy production env if .env doesn't exist
if [ ! -f .env ]; then
    cp .env.production .env
    echo "[1/6] Created .env from .env.production"
    echo ">>> IMPORTANT: Edit .env and set a strong SECRET_KEY!"
else
    echo "[1/6] .env already exists, skipping"
fi

# Install dependencies using the virtualenv pip
echo "[2/6] Installing Python dependencies..."
pip install -r requirements.txt

# Run migrations
echo "[3/6] Running database migrations..."
python manage.py migrate --noinput

# Collect static files
echo "[4/6] Collecting static files..."
python manage.py collectstatic --noinput

# Create superuser if needed
echo "[5/6] Creating superuser (skip if exists)..."
python manage.py createsuperuser --noinput 2>/dev/null || echo "Superuser already exists or DJANGO_SUPERUSER_* env vars not set"

echo "[6/6] Done! Restart the Python app from cPanel to apply changes."
echo ""
echo "=== Post-deployment checklist ==="
echo "1. Verify .env has a strong SECRET_KEY (not the default)"
echo "2. Restart Python app in cPanel > Setup Python App"
echo "3. Test: https://api.isoftke.co.ke/api/billing/packages/"
echo "4. Test: https://api.isoftke.co.ke/api/docs/"
