from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base, Session
from app.core import config


Base = declarative_base()

# engine = create_engine(
#     config.SQLALCHEMY_DATABASE_URI,
# )
# SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

class Database:
    def __init__(self, database_url: str):
        self.database_url = database_url
        self.engine = create_engine(self.database_url)
        self.SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=self.engine)
        self.Base = declarative_base()

    def get_db(self):
        db =self.SessionLocal()
        return db

tenantdb = Database(config.SQLALCHEMY_DATABASE_URI)


def get_tenant_db():
    db = tenantdb.get_db()
    try:
        yield db  
    finally:
        db.close() 

def get_user_db(database_url: str):
    database = Database(database_url)
    db = database.get_db()
    return db
