"""Litestar application factory."""

from litestar import Litestar, get
from litestar.logging import LoggingConfig
from litestar.openapi import OpenAPIConfig
from litestar.openapi.plugins import ScalarRenderPlugin
from config import settings
{% if advanced_alchemy %}
from advanced_alchemy.exceptions import RepositoryError
from config import alchemy
from lib import exception_handler
from controllers import UserController
{% endif %}
{% if litestar_vite %}
from litestar_vite import ViteConfig, VitePlugin
{% endif %}

@get("/health")
async def health() -> dict[str, str]:
    """Health check endpoint."""
    return {"status": "healthy"}


app = Litestar(
{% if advanced_alchemy %}
    route_handlers=[health, UserController],
{% else %}
    route_handlers=[health],
{% endif %}
    debug=settings.DEBUG,
    openapi_config=OpenAPIConfig(
        title=settings.APP_NAME,
        version="0.1.0",
        path="/api",
        render_plugins=[ScalarRenderPlugin()],
    ),
    logging_config=LoggingConfig(
        disable_stack_trace={
            404,
        },
        log_exceptions="always",
    ),
{% if advanced_alchemy %}
    exception_handlers={
        Exception: exception_handler,
        RepositoryError: exception_handler,
    },
{% endif %}
{% if advanced_alchemy and litestar_vite %}
    plugins=[alchemy, VitePlugin(config=ViteConfig(dev_mode=settings.DEBUG))],
{% elif advanced_alchemy %}
    plugins=[alchemy],
{% elif litestar_vite %}
    plugins=[VitePlugin(config=ViteConfig(dev_mode=settings.DEBUG))],
{% endif %}
)
