"""Quick check: list all tables in both SQLite DBs and show outfit-related data."""
import os
import sqlite3


def check_db(db_path, label):
    if not os.path.exists(db_path):
        print(f"\n❌ {label}: File not found at {db_path}")
        return

    size_mb = os.path.getsize(db_path) / (1024 * 1024)
    print(f"\n{'='*60}")
    print(f"📦 {label}")
    print(f"   Path: {db_path}")
    print(f"   Size: {size_mb:.1f} MB")
    print(f"{'='*60}")

    conn = sqlite3.connect(db_path)
    cur = conn.cursor()

    # List all tables
    cur.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
    tables = [r[0] for r in cur.fetchall()]
    print(f"\n📋 Tables ({len(tables)}):")
    for t in tables:
        cur.execute(f'SELECT COUNT(*) FROM "{t}"')
        cnt = cur.fetchone()[0]
        marker = " ⭐" if "outfit" in t.lower() else ""
        print(f"   • {t:50s}  ({cnt:,} rows){marker}")

    # Check outfit-specific tables
    outfit_tables = [t for t in tables if "outfit" in t.lower()]
    if outfit_tables:
        print(f"\n🎨 Outfit Tables Detail:")
        for t in outfit_tables:
            print(f"\n   >>> {t}")
            cur.execute(f'PRAGMA table_info("{t}")')
            cols = cur.fetchall()
            print(f"   Columns:")
            for c in cols:
                print(f"      {c[1]:20s}  {c[2]:15s}  {'PK' if c[5] else ''}")

            # Show sample data
            cur.execute(f'SELECT * FROM "{t}" LIMIT 5')
            sample = cur.fetchall()
            if sample:
                col_names = [c[1] for c in cols]
                print(f"   Sample data ({min(5, len(sample))} rows):")
                for row in sample:
                    print(f"      {dict(zip(col_names, row))}")
            else:
                print(f"   (empty table)")

    conn.close()

if __name__ == "__main__":
    base = os.path.dirname(os.path.abspath(__file__))
    backend = os.path.dirname(base)

    # Check 123.db
    check_db(os.path.join(backend, "123.db"), "123.db (Migration DB)")

    # Check canifa_ai_dump.sqlite
    check_db(os.path.join(base, "canifa_ai_dump.sqlite"), "canifa_ai_dump.sqlite (Main App DB)")
