from django import template
from django.conf import settings
from django.template.loader import render_to_string

register = template.Library()


@register.simple_tag
def turnstile_widget():
    """
    Template tag to render the Cloudflare Turnstile widget.
    When DEBUG is True, renders an empty template.
    When DEBUG is False, renders the normal widget.
    Usage: {% load turnstile_tags %}{% turnstile_widget %}
    """
    if settings.DEBUG:
        template_name = "accounts/turnstile_widget_empty.html"
        context = {}
    else:
        template_name = "accounts/turnstile_widget.html"
        context = {"site_key": settings.TURNSTILE_SITE_KEY}

    return render_to_string(template_name, context)
