from sqlmodel import SQLModel, create_engine, Session
import os

# Use SQLite for local dev by default, or override with DATABASE_URL env var
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./database.db")

# check_same_thread is needed for SQLite
connect_args = {"check_same_thread": False} if "sqlite" in DATABASE_URL else {}

engine = create_engine(DATABASE_URL, echo=True, connect_args=connect_args)

def create_db_and_tables():
    SQLModel.metadata.create_all(engine)

def get_session():
    with Session(engine) as session:
        yield session
