import os

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base

# On the server, set DATABASE_URL in the environment (systemd / shell) so the
# password is not committed in source. Falls back to the local dev database.
DATABASE_URL = os.getenv(
    "DATABASE_URL",
    "postgresql://health_monitoring_user:Health_Monitoring2026@localhost:5432/health_monitoring_db",
)

engine = create_engine(
    DATABASE_URL,
    echo=False,
)

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

Base = declarative_base()

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