from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from dotenv import load_dotenv
import os

# Nur im Dev-Modus .env laden
if os.getenv("ENV", "development") == "development":
    load_dotenv(dotenv_path=os.path.join(
        os.path.dirname(__file__), '..', '.env'))

# Prod: DB_CONN direkt aus Umgebungsvariable (von Compose gesetzt)
DB_URL = os.getenv("DB_CONN")
if not DB_URL:
    # Dev: DB-URL aus Einzelwerten bauen
    DB_USER = os.getenv("DB_USER", "infoscreen_admin")
    DB_PASSWORD = os.getenv("DB_PASSWORD", "KqtpM7wmNd&mFKs")
    # Dev container: use host.docker.internal or localhost if db container isn't on same network
    # Docker Compose: use 'db' service name
    DB_HOST = os.getenv("DB_HOST", "db")  # Default to db for Docker Compose
    DB_NAME = os.getenv("DB_NAME", "infoscreen_by_taa")
    DB_URL = f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}/{DB_NAME}"

print(f"Using DB_URL: {DB_URL}")  # Debug-Ausgabe
engine = create_engine(DB_URL, echo=False)
Session = sessionmaker(bind=engine)
