from abc import ABC, abstractmethod

from robyn.status_codes import StatusCodes

from .robyn import Response

from jinja2 import Environment, FileSystemLoader


class TemplateInterface(ABC):
    def __init__(self):
        ...

    @abstractmethod
    def render_template(self, *args, **kwargs) -> Response:
        ...


class JinjaTemplate(TemplateInterface):
    def __init__(self, directory, encoding="utf-8", followlinks=False):
        self.env = Environment(
            loader=FileSystemLoader(
                searchpath=directory, encoding=encoding, followlinks=followlinks
            )
        )

    def render_template(self, template_name, **kwargs) -> Response:
        rendered_template = self.env.get_template(template_name).render(**kwargs)
        return Response(
            status_code=StatusCodes.HTTP_200_OK.value,
            body=rendered_template,
            headers={"Content-Type": "text/html; charset=utf-8"},
        )


__all__ = ["TemplateInterface", "JinjaTemplate"]
