"""The FastAPI app factory — mount the brand kit, wire Jinja, register the routes.

``create_app()`` builds the ASGI app: the shared ``mgf-brand-web`` static kit at
``/static/brand`` (the single source of truth for Magogi styling, mounted not
vendored), the app's own ``app.css`` at ``/static``, the Jinja templates, and the
read-only routes (federation overview + per-project lenses).
"""

from __future__ import annotations

from pathlib import Path

from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from mgf.brand_web import static_dir as brand_static_dir

from .routes import build_router

_HERE = Path(__file__).parent
_TEMPLATES = _HERE / "templates"
_STATIC = _HERE / "static"


def create_app() -> FastAPI:
    """Build the federation-dashboard FastAPI app (read-only; binds localhost)."""
    app = FastAPI(title="mgf-fed-web", docs_url=None, redoc_url=None)
    # Mount the brand kit FIRST so its more-specific /static/brand prefix wins over
    # the app's own /static mount (which carries only app.css — the shell layout the
    # kit doesn't provide; every component class comes from the kit).
    app.mount("/static/brand", StaticFiles(directory=str(brand_static_dir())), name="brand")
    app.mount("/static", StaticFiles(directory=str(_STATIC)), name="static")

    templates = Jinja2Templates(directory=str(_TEMPLATES))
    app.include_router(build_router(templates))
    return app
