from collections.abc import AsyncIterator

from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase

from app.infra.settings import get_settings


class Base(DeclarativeBase):
    """Base declarative model class."""


settings = get_settings()
async_engine = create_async_engine(settings.database_url, echo=False)
SessionLocal = async_sessionmaker(bind=async_engine, class_=AsyncSession, expire_on_commit=False)


async def get_db_session() -> AsyncIterator[AsyncSession]:
    async with SessionLocal() as session:
        yield session


async def get_db_session_context() -> AsyncIterator[AsyncSession]:
    """Context manager version of get_db_session for use in lifespan events."""
    async with SessionLocal() as session:
        yield session
