import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from sqlalchemy import create_engine, text, inspect
from app.config import get_settings

settings = get_settings()
engine = create_engine(settings.database_url)
insp = inspect(engine)
tables = sorted(insp.get_table_names())

print(f"{'TABLE':40s} {'ROWS':>6s}")
print("-" * 50)
total = 0
with engine.connect() as conn:
    for t in tables:
        count = conn.execute(text(f"SELECT COUNT(*) FROM {t}")).scalar()
        if count > 0:
            print(f"{t:40s} {count:>6d}")
            total += count

print("-" * 50)
print(f"Total tables with data: {sum(1 for t in tables if conn.execute(text(f'SELECT COUNT(*) FROM {t}')).scalar() > 0)}")
print(f"Total rows across all tables: {total}")
