from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from sqlalchemy.orm import DeclarativeBase
import os
from dotenv import load_dotenv
load_dotenv()
class Model(DeclarativeBase):
    pass

async_engine = create_async_engine(
    os.getenv("SQL_URL"),
    connect_args={"check_same_thread": False}
    )


async_session = async_sessionmaker(
        async_engine,
        autoflush=True,
        autocommit=False, 
        expire_on_commit =False
    )


async def connect() -> None:
    async with async_engine.begin() as conn:
        await conn.run_sync(Model.metadata.create_all, checkfirst=True)

async def disconnect() -> None:
    if async_engine:
        await async_engine.dispose()