from typing import AsyncGenerator
from .database import db  
from motor.motor_asyncio import AsyncIOMotorClient

# Dependency to get the MongoDB client connection (db)
async def get_db() -> AsyncGenerator[AsyncIOMotorClient, None]:
    try:
        # Yielding the database connection to be used in the routes
        yield db
    finally:
        # Close the connection after the request is complete (MongoDB client doesn't require manual close)
        # `client.close()` is not needed because MongoDB client is long-lived in an async environment.
        pass
