import sqlite3
import sys

def debug_database():
    sys.stdout.reconfigure(encoding='utf-8')
    conn = sqlite3.connect('store_bot.db')
    cursor = conn.cursor()
    
    print("=== Categories ===")
    cursor.execute("SELECT id, name, parent_id FROM categories")
    for row in cursor.fetchall():
        print(f"Cat ID: {row[0]} | Name: {row[1]} | Parent: {row[2]}")
        
    print("\n=== Products ===")
    cursor.execute("SELECT id, category_id, name, price, stock FROM products")
    products = cursor.fetchall()
    for row in products:
        print(f"Prod ID: {row[0]} | Cat: {row[1]} | Name: {row[2]} | Price: {row[3]} | Stock: {row[4]}")
        
    print("\n=== Gift Cards Summary ===")
    cursor.execute("SELECT product_id, is_sold, count(*) FROM gift_cards GROUP BY product_id, is_sold")
    gc_summary = cursor.fetchall()
    if not gc_summary:
        print("No gift cards found in the database!")
    for row in gc_summary:
        print(f"Prod ID: {row[0]} | Is Sold: {row[1]} | Count: {row[2]}")
        
    print("\n=== Detailed Gift Cards (Unsold) ===")
    cursor.execute("SELECT id, product_id, code, is_sold FROM gift_cards WHERE is_sold = 0 LIMIT 10")
    unsold_gcs = cursor.fetchall()
    for row in unsold_gcs:
        print(f"GC ID: {row[0]} | Prod ID: {row[1]} | Code: {row[2]} | Is Sold: {row[3]}")
        
    conn.close()

if __name__ == '__main__':
    debug_database()
