"""
Diagnostic: check old PHP tables vs new Django tables for payment data.
Run: python check_data.py
"""
import django
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from django.db import connection
import json

c = connection.cursor()

print("=" * 60)
print("1. OLD PHP 'payments' table - recent entries")
print("=" * 60)
c.execute("SELECT id, account, amount_to_pay, reference, tx_id, pay_date FROM payments ORDER BY id DESC LIMIT 10")
cols = [d[0] for d in c.description]
rows = c.fetchall()
if rows:
    for r in rows:
        print(" ", dict(zip(cols, r)))
else:
    print("  (empty)")

print()
print("=" * 60)
print("2. NEW Django 'repayments' table - all entries")
print("=" * 60)
c.execute("SELECT receipt_number, amount, payment_source, mpesa_transaction_id, created_at FROM repayments ORDER BY created_at DESC LIMIT 10")
cols = [d[0] for d in c.description]
rows = c.fetchall()
if rows:
    for r in rows:
        print(" ", dict(zip(cols, r)))
else:
    print("  (empty)")

print()
print("=" * 60)
print("3. SMS LOGS - all entries")
print("=" * 60)
c.execute("SELECT sms_type, borrower_name, loan_number, amount, status, created_at FROM sms_logs ORDER BY created_at DESC LIMIT 10")
cols = [d[0] for d in c.description]
rows = c.fetchall()
if rows:
    for r in rows:
        print(" ", dict(zip(cols, r)))
else:
    print("  (empty)")

print()
print("=" * 60)
print("4. IPN LOGS - all entries")
print("=" * 60)
c.execute("SELECT id, raw_data, created_at FROM sasapay_ipn_logs ORDER BY created_at DESC LIMIT 10")
cols = [d[0] for d in c.description]
rows = c.fetchall()
if rows:
    for raw_data, created_at in [(r[1], r[2]) for r in rows]:
        d = json.loads(raw_data) if isinstance(raw_data, str) else raw_data
        print(f"  [{created_at}] TransID={d.get('TransID')} BillRef={d.get('BillRefNumber')} Amount={d.get('TransAmount')}")
else:
    print("  (empty)")

print()
print("=" * 60)
print("5. SASAPAY UNKNOWN PAYMENTS")
print("=" * 60)
c.execute("SELECT amount, paid_by, msisdn, bill_ref, reference, notes, created_at FROM sasapay_unknown_payments ORDER BY created_at DESC LIMIT 10")
cols = [d[0] for d in c.description]
rows = c.fetchall()
if rows:
    for r in rows:
        print(" ", dict(zip(cols, r)))
else:
    print("  (empty)")

print()
print("=" * 60)
print("6. LOAN_INFO table (old PHP) - recent active loans")
print("=" * 60)
try:
    c.execute("DESCRIBE loan_info")
    loan_info_cols = [r[0] for r in c.fetchall()]
    print("  Columns:", loan_info_cols[:10], "...")
    c.execute("SELECT * FROM loan_info ORDER BY id DESC LIMIT 3")
    cols = [d[0] for d in c.description]
    for r in c.fetchall():
        print(" ", dict(zip(cols, r)))
except Exception as e:
    print(f"  Error: {e}")

print()
print("=" * 60)
print("7. BORROWERS table (old PHP) - check phone format")
print("=" * 60)
try:
    c.execute("DESCRIBE borrowers")
    bor_cols = [r[0] for r in c.fetchall()]
    print("  Columns:", bor_cols)
    c.execute("SELECT id, phone, status FROM borrowers ORDER BY id DESC LIMIT 5")
    cols = [d[0] for d in c.description]
    for r in c.fetchall():
        print(" ", dict(zip(cols, r)))
except Exception as e:
    print(f"  Error: {e}")

print()
print("=" * 60)
print("8. SASAPAY IPN URL registered in settings")
print("=" * 60)
from django.conf import settings
site_url = getattr(settings, 'SITE_URL', 'NOT SET')
print(f"  SITE_URL: {site_url}")
print(f"  IPN URL:  {site_url}/payments/sasapay/ipn/")
