"""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 src.backend.config import settings
{% if advanced_alchemy %}
from advanced_alchemy.exceptions import RepositoryError
from src.backend.config import alchemy
from src.backend.lib import exception_handler
from src.backend.controllers import UserController
{% endif %}
{% if litestar_vite %}
from litestar_vite import ViteConfig, VitePlugin
from src.backend.config import vite_config
{% endif %}
{% if has_store %}
from src.backend.config import redis_store
{% endif %}
{% if litestar_saq %}
from src.backend.config import saq
{% endif %}
{% if litestar_granian %}
from litestar_granian import GranianPlugin
{% 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="/schema",
        render_plugins=[ScalarRenderPlugin()],
    ),
    logging_config=LoggingConfig(
        disable_stack_trace={
            404,
        },
        log_exceptions="always",
    ),
{% if has_store %}
    stores={"redis": redis_store},
{% endif %}
{% if advanced_alchemy %}
    exception_handlers={
        Exception: exception_handler,
        RepositoryError: exception_handler,
    },
{% endif %}
    plugins=[
{% if advanced_alchemy %}
        alchemy,
{% endif %}
{% if litestar_vite %}
        VitePlugin(config=ViteConfig(dev_mode=settings.DEBUG)),
{% endif %}
{% if litestar_saq %}
        saq,
{% endif %}
{% if litestar_granian %}
        GranianPlugin(),
{% endif %}
    ],
)
