from worker_app.core.config import get_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

settings = get_settings()
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)
