"""{{ project_name }} application entrypoint."""

from __future__ import annotations

from collections.abc import AsyncIterator
from contextlib import asynccontextmanager

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

# <<<inject:imports>>>

try:
    # canonical health package
    from src.health import register_health_routes as _core_register_health_routes
except ImportError:  # pragma: no cover - health package not generated yet

    def _register_health_routes(_: FastAPI) -> None:
        return None

else:

    def _register_health_routes(app: FastAPI) -> None:
        try:
            _core_register_health_routes(app)
        except Exception:  # pragma: no cover - defensive best-effort registration
            return None


from .routing import api_router


@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
    """Coordinate startup/shutdown hooks contributed by RapidKit modules."""

    _ = app  # ensure the app reference stays available for injected hooks
    # <<<inject:startup>>>
    try:
        yield
    finally:
        # <<<inject:shutdown>>>
        pass


app = FastAPI(
    title="{{ project_name }}",
    description="{{ description }}",
    version="{{ app_version }}",
    docs_url="/docs",
    redoc_url="/redoc",
    lifespan=lifespan,
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(api_router, prefix="/api")
# Expose module-provided health routers under /api/health/module/*
_register_health_routes(app)
# <<<inject:routes>>>
