import os
from sqlmodel import create_engine, Session


if os.getenv("POSTGRES_HOST"):
    engine = create_engine(f"postgresql://{os.getenv('POSTGRES_USER')}:{os.getenv('POSTGRES_PASSWORD')}@{os.getenv('POSTGRES_HOST')}/{os.getenv('POSTGRES_DB')}",
                           echo=False, future=True)
else:
    sqlite_file_name = os.getenv("SQLITE_DB_PATH", "eveal.db")
    engine = create_engine(f"sqlite:///{sqlite_file_name}", echo=True, future=True, connect_args={"check_same_thread": False})

def get_session():
    db = Session(engine)
    try:
        yield db
    finally:
        db.close()
