from app.config import settings

from packages.adapters.sqlalchemy_impl import (
    build_session_factory,
    ensure_database_exists,
    initialize_database,
)
from packages.adapters.sqlalchemy_impl.schema_guard import assert_auto_create_schema_allowed

ensure_database_exists(settings.DATABASE_URL)
engine, SessionLocal = build_session_factory(
    settings.DATABASE_URL,
    pool_size=settings.DATABASE_POOL_SIZE,
    max_overflow=settings.DATABASE_MAX_OVERFLOW,
    pool_timeout=settings.DATABASE_POOL_TIMEOUT,
    pool_recycle=settings.DATABASE_POOL_RECYCLE,
)
assert_auto_create_schema_allowed(settings.ENVIRONMENT, settings.AUTO_CREATE_SCHEMA)
if settings.AUTO_CREATE_SCHEMA:
    initialize_database(engine)
