from typing import AsyncGenerator

from fastapi import Depends
from fastapi_users_db_sqlalchemy import SQLAlchemyUserDatabase
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

from defi_common import dbconfig
from defi_common.database.models import User

DATABASE_URL = dbconfig.db_config.db_url

engine = create_async_engine(DATABASE_URL, echo=False)
Base = declarative_base()
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)


async def init_models() -> None:
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.drop_all)
        await conn.run_sync(Base.metadata.create_all)


# Dependency
async def get_session() -> AsyncGenerator:  # type: ignore
    async with async_session() as session:
        yield session


async def get_user_db(session: AsyncSession = Depends(get_session)) -> AsyncGenerator: #type: ignore
    yield SQLAlchemyUserDatabase(session, User)
