import os

from dotenv import load_dotenv
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session

from app.infrastructure.database.models import Base

load_dotenv()
SQLALCHEMY_DATABASE_URL = os.getenv("DATABASE_URL")

engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)


def connect() -> Session:
    global SessionLocal
    return SessionLocal()


def init_db():
    import app.infrastructure.database.models.roles
    import app.infrastructure.database.models.users
    import app.infrastructure.database.models.statuses
    import app.infrastructure.database.models.federal_districts
    import app.infrastructure.database.models.trucks
    import app.infrastructure.database.models.cities
    import app.infrastructure.database.models.accessory
    import app.infrastructure.database.models.total_orders
    import app.infrastructure.database.models.delivery_orders
    import app.infrastructure.database.models.delivery_accessories

    Base.metadata.create_all(bind=engine)
