from fastapi import FastAPI

from backend.config import settings
from backend.db import database, Base, engine
from backend.routes import shortener_router

app = FastAPI()

@app.on_event("startup")
async def startup():
    await database.connect()

    # only create missing tables in dev environment
    if settings.environment == "dev":
        Base.metadata.create_all(bind=engine)

@app.on_event("shutdown")
async def shutdown():
    await database.disconnect()

app.include_router(shortener_router)
