from contextlib import asynccontextmanager
from fastapi import FastAPI
from app.database import Base, engine
from app.controllers import {{ default_resource }}_controller
# V-- Auto-include new controllers here --V  

# Asynchronous lifespan manager to handle startup events
@asynccontextmanager
async def lifespan(app: FastAPI):
    {% if is_async %}
    # For async projects, create tables on startup
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)
    {% endif %}
    yield
    # Code in this block would run on shutdown

app = FastAPI(
    title="{{ project_name }}",
    description="A project generated by fast-boiler.",
    version="1.0.0",
    lifespan=lifespan
)

# Include routers from the controllers
app.include_router({{ default_resource }}_controller.router)
# V-- Auto-include new routers here --V \

@app.get("/", tags=["Root"])
async def read_root():
    return {"message": "Welcome to the API!"}

{% if not is_async %}
# For sync projects, create tables here before the app starts
Base.metadata.create_all(bind=engine)
{% endif %}