"""saju-lab FastAPI app."""
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from .config import CORS_ALLOW_ORIGINS
from .routers import saju, compat
from . import db as db_module


app = FastAPI(title="saju-lab")

_origins = [o.strip() for o in CORS_ALLOW_ORIGINS.split(",") if o.strip()]
app.add_middleware(
    CORSMiddleware,
    allow_origins=_origins,
    allow_credentials=False,
    allow_methods=["GET", "POST", "PATCH", "DELETE", "OPTIONS"],
    allow_headers=["Content-Type"],
)


@app.on_event("startup")
def _init():
    db_module.init_db()


@app.get("/health")
def health():
    return {"ok": True}


app.include_router(saju.router)
app.include_router(compat.router)
