"""
Check what tables exist in the database
"""
import os
import django

# Setup Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from django.db import connection

def list_tables():
    """List all tables in the database"""
    with connection.cursor() as cursor:
        cursor.execute("""
            SELECT table_name 
            FROM information_schema.tables 
            WHERE table_schema = DATABASE()
            ORDER BY table_name
        """)
        tables = cursor.fetchall()
        
        print(f"Database: {connection.settings_dict['NAME']}")
        print(f"\nTotal tables: {len(tables)}\n")
        print("Tables in database:")
        print("-" * 50)
        
        for table in tables:
            print(f"  - {table[0]}")
        
        # Check for specific tables we need
        print("\n" + "=" * 50)
        print("Checking for required tables:")
        print("=" * 50)
        
        required_tables = [
            'users_customuser',
            'utils_auditlog',
            'loans',
            'loan_applications',
        ]
        
        table_names = [t[0] for t in tables]
        
        for req_table in required_tables:
            exists = req_table in table_names
            status = "✓ EXISTS" if exists else "✗ MISSING"
            print(f"{status}: {req_table}")

if __name__ == '__main__':
    list_tables()
