from chitai.config import settings

from advanced_alchemy.extensions.litestar import (
    SQLAlchemyAsyncConfig,
    AsyncSessionConfig,
    SQLAlchemyPlugin,
)
from advanced_alchemy.base import BigIntAuditBase
from sqlalchemy.ext.asyncio import create_async_engine
from chitai.database import models  # noqa: F401  # Import to register models

DATABASE_URL = str(settings.postgres_uri)

session_config = AsyncSessionConfig(expire_on_commit=False)

config = SQLAlchemyAsyncConfig(
    engine_instance=create_async_engine(DATABASE_URL, echo=settings.postgres_echo),
    session_config=session_config,
    before_send_handler="autocommit",
    create_all=False,
    metadata=BigIntAuditBase.registry.metadata,
)

alchemy = SQLAlchemyPlugin(config=config)
