"""
VibeDB Quickstart Example

Demonstrates basic CRUD operations with VibeDB.
"""

from vibedb import VibeDB


def main():
    print("🚀 VibeDB Quickstart Example\n")

    # Create database (in-memory for this example)
    db = VibeDB(":memory:")
    print("✓ Database created")

    # Get collection (auto-created)
    todos = db.collection("todos")
    print("✓ Collection 'todos' ready")

    # Insert documents
    print("\n📝 Inserting todos...")
    id1 = todos.insert({"task": "Buy milk", "done": False, "priority": "high"})
    id2 = todos.insert({"task": "Write code", "done": True, "priority": "high"})
    id3 = todos.insert({"task": "Read book", "done": False, "priority": "low"})
    print(f"  ✓ Inserted 3 todos")

    # Count documents
    count = todos.count()
    print(f"\n📊 Total todos: {count}")

    # Get document by ID
    print(f"\n🔍 Getting todo by ID...")
    todo = todos.get(id1)
    if todo:
        print(f"  Task: {todo.task}")
        print(f"  Done: {todo.done}")
        print(f"  Priority: {todo.priority}")

    # Update document
    print(f"\n✏️  Updating todo...")
    todos.update(id1, {"done": True})
    updated = todos.get(id1)
    print(f"  Task '{updated.task}' is now done: {updated.done}")

    # Find all documents
    print(f"\n📋 All todos:")
    for todo in todos.find():
        status = "✓" if todo.done else "○"
        print(f"  {status} [{todo.priority}] {todo.task}")

    # Find with simple filter (exact match)
    print(f"\n🔎 Incomplete todos:")
    for todo in todos.find({"done": False}):
        print(f"  • {todo.task}")

    # Delete document
    print(f"\n🗑️  Deleting a todo...")
    todos.delete(id3)
    print(f"  ✓ Deleted")

    # Final count
    count = todos.count()
    print(f"\n📊 Final count: {count} todos")

    # Database stats
    print(f"\n📈 Database stats:")
    stats = db.stats()
    print(f"  Collections: {stats['collection_count']}")
    print(f"  Total documents: {stats['total_documents']}")

    # Cleanup
    db.close()
    print(f"\n✓ Database closed")


if __name__ == "__main__":
    main()
