"""FastAPI application entry point for {{ cookiecutter.project_name }}."""

from __future__ import annotations

from contextlib import asynccontextmanager
from pathlib import Path

from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles

from {{ cookiecutter.project_name }}.config.logger import setup_logging
{% if cookiecutter.use_db == "yes" %}
from {{ cookiecutter.project_name }}.db.connection import close_db, init_db
{% endif %}
from {{ cookiecutter.project_name }}.api.router import api_router

_BASE_DIR = Path(__file__).resolve().parent


@asynccontextmanager
async def lifespan(app: FastAPI):
    {% if cookiecutter.use_db == "yes" %}setup_logging()
    await init_db()
    try:
        yield
    finally:
        await close_db(){% else %}setup_logging()
    yield{% endif %}


app = FastAPI(lifespan=lifespan)

static_dir = _BASE_DIR / "static"
static_dir.mkdir(exist_ok=True)
app.mount("/static", StaticFiles(directory=static_dir), name="static")

app.include_router(api_router, prefix="/api")


@app.get("/health")
async def health() -> dict[str, str]:
    return {"status": "ok"}