"""
Every SiteSettings row on this install, and which one the panel writes to.

Run inside the install, from its backend directory:

    venv/bin/python manage.py shell --settings=config.settings.production < deploy/check_settings_rows.py

Reads only. Changes nothing. No secret value is printed, only lengths.

Why this exists: the Twilio auth token was saved through the panel and the
value read back by a script was unchanged. The save path is correct, so the
next suspect is that the two are reading different rows. SiteSettings is
per tenant, and a script picking the master tenant does not necessarily pick
the same row a browser request resolves.
"""
from apps.pages.models import SiteSettings
from apps.tenants.models import Tenant

print()
print('── Tenants ─────────────────────────────────────────────────────────────')
for t in Tenant.objects.order_by('pk'):
    print(f'  pk={t.pk:<4} is_master={str(getattr(t, "is_master", "?")):<5} '
          f'{t.name}  ({getattr(t, "primary_domain", "") or "no domain"})')

print()
print('── SiteSettings rows ───────────────────────────────────────────────────')
print(f'  {"pk":<4} {"tenant":<28} {"sid":>5} {"token":>6}  {"GA id":<16}')
print('  ' + '-' * 68)
for s in SiteSettings.objects.order_by('pk'):
    t = getattr(s, 'tenant', None)
    label = f'{t.pk}:{t.name}' if t else 'no tenant'
    sid = (getattr(s, 'twilio_account_sid', '') or '')
    tok = (getattr(s, 'twilio_auth_token', '') or '')
    ga = (getattr(s, 'google_analytics_id', '') or '') or '-'
    print(f'  {s.pk:<4} {label[:28]:<28} {len(sid):>5} {len(tok):>6}  {ga:<16}')

print()
master = Tenant.objects.filter(is_master=True).first()
print(f'  master tenant       : {master.pk if master else "none"} '
      f'{master.name if master else ""}')
print(f'  masters found       : {Tenant.objects.filter(is_master=True).count()}')
print(f'  SiteSettings.load() : row {SiteSettings.load(master).pk}')
print()
print('If a row other than that one holds a 32 character token, the panel is')
print('writing there and the reader is looking at the wrong place.')
print()
