"""
VibeDB Studio UI Test

Tests the Studio web interface by launching it with sample data.
"""

from vibedb import VibeDB

def main():
    print("🎨 VibeDB Studio UI Test\n")

    # Create test database with sample data
    db = VibeDB("studio_test.db")

    # Create users collection
    users = db.collection("users")
    users.delete_many({})  # Clear existing data

    print("📝 Creating sample data...")

    # Insert sample users
    user_ids = users.insert_many([
        {"name": "Alice Johnson", "age": 28, "email": "alice@example.com", "role": "admin", "tags": ["python", "ml"]},
        {"name": "Bob Smith", "age": 35, "email": "bob@example.com", "role": "user", "tags": ["javascript", "react"]},
        {"name": "Charlie Brown", "age": 22, "email": "charlie@example.com", "role": "user", "tags": ["python", "django"]},
        {"name": "Diana Prince", "age": 30, "email": "diana@example.com", "role": "moderator", "tags": ["java", "spring"]},
        {"name": "Eve Anderson", "age": 26, "email": "eve@example.com", "role": "user", "tags": ["python", "flask"]},
    ])
    print(f"  ✓ Created {len(user_ids)} users")

    # Create products collection
    products = db.collection("products")
    products.delete_many({})

    product_ids = products.insert_many([
        {"name": "Laptop", "price": 999.99, "category": "electronics", "stock": 50},
        {"name": "Mouse", "price": 29.99, "category": "electronics", "stock": 200},
        {"name": "Desk", "price": 299.99, "category": "furniture", "stock": 30},
        {"name": "Chair", "price": 199.99, "category": "furniture", "stock": 45},
        {"name": "Monitor", "price": 399.99, "category": "electronics", "stock": 75},
    ])
    print(f"  ✓ Created {len(product_ids)} products")

    # Create orders collection
    orders = db.collection("orders")
    orders.delete_many({})

    order_ids = orders.insert_many([
        {"user_id": user_ids[0], "product_ids": [product_ids[0], product_ids[1]], "total": 1029.98, "status": "completed"},
        {"user_id": user_ids[1], "product_ids": [product_ids[2]], "total": 299.99, "status": "pending"},
        {"user_id": user_ids[2], "product_ids": [product_ids[3], product_ids[4]], "total": 599.98, "status": "completed"},
    ])
    print(f"  ✓ Created {len(order_ids)} orders")

    # Create some slow queries for index suggestions
    print("\n🔍 Generating query patterns for index suggestions...")
    for i in range(10):
        users.find({"age": {"$gte": 25}})
        users.find({"role": "user"})
        products.find({"category": "electronics"})
        products.find({"price": {"$lte": 300}})
    print("  ✓ Executed 40 queries")

    # Create an index as example
    users.create_index(["email"], unique=True)
    print("  ✓ Created index on users.email")

    # Display stats
    stats = db.stats()
    print(f"\n📊 Database Stats:")
    print(f"  Collections: {stats['collection_count']}")
    print(f"  Total Documents: {stats['total_documents']}")

    # Launch Studio
    print("\n🚀 Launching Studio UI...")
    print("  URL: http://localhost:5000")
    print("  Press Ctrl+C to stop\n")
    print("Things to try in Studio:")
    print("  • Browse collections in the sidebar")
    print("  • View documents in the Documents tab")
    print("  • Run queries in the Query tab (try: {\"age\": {\"$gte\": 25}})")
    print("  • View index suggestions in the Indexes tab\n")

    db.studio(port=5000)


if __name__ == "__main__":
    main()
