#!/bin/bash
#
# Everything written since 26.8.5 went out, in one deploy.
#
#   pages/0036     FAB Club OAuth pair, Google Analytics service account
#   tenants/0022   per-install reference prefix, backfilled in the same run
#
# Plus the code that reads them: references now come from apps/tenants/refs.py
# instead of five hardcoded copies of 'FT-', FAB and Analytics read the
# database instead of .env and a JSON file on disk.
#
# Migrations first, confirmed applied, then the code. models.py gains fields in
# both apps, and Django puts every field into the SELECT for every query on
# that table, so the order is not negotiable.
#
# Usage, from the Mac:
#   bash deploy/ship-pending.sh
#
set -euo pipefail

SERVER="root@194.76.26.170"
REMOTE="/home/finaltarget/public_html/backend"
LOCAL="$(cd "$(dirname "$0")/.." && pwd)"

MIGRATIONS="apps/pages/migrations/0036_sitesettings_fab_and_ga.py
apps/tenants/migrations/0022_tenant_ref_prefix.py"

CODE="apps/pages/models.py
apps/tenants/models.py
apps/tenants/refs.py
apps/fab_auth/backend.py
apps/accounts/ga_utils.py
apps/accounts/cms_views.py
apps/tickets/models.py
apps/tickets/management/commands/fetch_ticket_replies.py
apps/ecommerce/models.py
apps/chat/models.py
apps/contact/views.py
config/settings/base.py
config/settings/development.py
config/settings/production.py
templates/cms/settings.html
templates/cms/tenants/edit.html
deploy/null-check.sh
deploy/upgrade-django.sh"

echo "── 1. Migrations first, on their own ───────────────────────────────────"
cd "$LOCAL"
rsync -av --relative $MIGRATIONS "${SERVER}:${REMOTE}/"
ssh "$SERVER" "cd ${REMOTE} && venv/bin/python manage.py migrate --settings=config.settings.production"

echo
echo "── 2. Confirm before any code that reads them ──────────────────────────"
ssh "$SERVER" "cd ${REMOTE} && venv/bin/python manage.py showmigrations pages tenants --settings=config.settings.production | grep -E '0036_sitesettings_fab_and_ga|0022_tenant_ref_prefix'"
read -r -p "Both marked [X]? Continue with the code? [y/N] " reply
[ "$reply" = "y" ] || [ "$reply" = "Y" ] || { echo "Stopped. Nothing else sent."; exit 1; }

echo
echo "── 3. Code ─────────────────────────────────────────────────────────────"
rsync -av --relative $CODE "${SERVER}:${REMOTE}/"

echo
echo "── 4. Restart ──────────────────────────────────────────────────────────"
ssh "$SERVER" "systemctl restart finaltarget && sleep 3 && systemctl is-active finaltarget"

echo
echo "── 5. Did the backfill give every install its own letters ──────────────"
ssh "$SERVER" "cd ${REMOTE} && venv/bin/python manage.py shell --settings=config.settings.production -c \"
from apps.tenants.models import Tenant
for t in Tenant.objects.order_by('-is_master','name')[:12]:
    print(f'  {t.prefix:6} {\\\"master\\\" if t.is_master else \\\"      \\\"}  {t.name}')
\""

echo
echo "── 6. Pages ────────────────────────────────────────────────────────────"
curl -s -o /dev/null -w "  panel   %{http_code}\n" https://panel.finaltarget.com/system/
curl -s -o /dev/null -w "  site    %{http_code}\n" https://finaltarget.com/

cat <<'NOTE'

Check the list above says FT against Final Target. Anything else there means
the backfill read a slug it did not expect, and the tenant page lets you
correct it before a single reference is issued.

Then, in the panel at /settings/, one thing only:
  SEO and Analytics    paste the whole ga_credentials.json into Service Account

Until that is saved the dashboard charts are off.

The FAB Club card stays empty. FabClub does not exist yet, nothing links to
the sign in routes, and blank credentials break nothing. Fill it in the day
FAB goes live.
NOTE
