"""Minimal template renderer shim for reactive routing.

Wraps :class:`~cleveragents.templates.secure_renderer.SecureTemplateRenderer`
to satisfy the existing import contract while enforcing security controls.
"""

from __future__ import annotations

import logging
from enum import Enum
from typing import Any

from cleveragents.templates.secure_renderer import (
    SecureTemplateRenderer,
    TemplateConfig,
    TemplateError,
)

logger = logging.getLogger(__name__)


class TemplateEngine(Enum):
    """Supported template engines."""

    SIMPLE = "simple"


class TemplateRenderer:
    """Thin wrapper over :class:`SecureTemplateRenderer`.

    The ``render`` method silently returns the raw template on
    any rendering failure to preserve backward compatibility.

    Args:
        engine: Template engine selector (only ``SIMPLE`` is supported).
        config: Optional :class:`TemplateConfig` forwarded to the
            underlying :class:`SecureTemplateRenderer`.  When ``None``
            the default (open-allowlist) config is used.
    """

    def __init__(
        self,
        engine: TemplateEngine = TemplateEngine.SIMPLE,
        config: TemplateConfig | None = None,
    ) -> None:
        self.engine = engine
        self._secure = SecureTemplateRenderer(config=config or TemplateConfig())

    def render(self, template: str, context: dict[str, Any] | None = None) -> str:
        """Render *template* with *context*, returning raw on error."""
        context = context or {}
        try:
            return self._secure.render(template, context)
        except TemplateError:
            logger.warning(
                "Template rendering failed; returning raw template "
                "(length=%d). Enable DEBUG logging for details.",
                len(template),
            )
            logger.debug("Suppressed TemplateError for template: %r", template)
            return template
