"""General-purpose email service.

⚠️ AUTO-GENERATED BY PRISM (GENERATE_ONCE)
This file was generated once. You can safely edit it — Prism will not overwrite.
Add custom send functions, business logic emails, and integrations here.
"""

from __future__ import annotations

import logging
from typing import Any

from {{ project_name }}.email.client import send_email, send_email_async, send_email_fire_and_forget
from {{ project_name }}.email.templates import render_template

logger = logging.getLogger(__name__)


# ── Convenience helpers ──────────────────────────────────────────

def send_templated_email(
    to: str | list[str],
    template_name: str,
    subject: str,
    context: dict[str, Any] | None = None,
    **kwargs: Any,
) -> dict[str, Any] | None:
    """Send an email using an HTML template.

    Args:
        to: Recipient email address(es).
        template_name: Template name (without .html extension).
        subject: Email subject line.
        context: Template variables.
        **kwargs: Additional arguments forwarded to send_email().

    Returns:
        Resend API response dict on success, None on failure.
    """
    html = render_template(template_name, context or {})
    return send_email(to, subject, html, **kwargs)


async def send_templated_email_async(
    to: str | list[str],
    template_name: str,
    subject: str,
    context: dict[str, Any] | None = None,
    **kwargs: Any,
) -> dict[str, Any] | None:
    """Send a templated email asynchronously."""
    html = render_template(template_name, context or {})
    return await send_email_async(to, subject, html, **kwargs)


{% for tmpl in templates %}
def send_{{ tmpl.name }}(
    to: str | list[str],
{% for var in tmpl.variables %}
    {{ var }}: str,
{% endfor %}
    **kwargs: Any,
) -> dict[str, Any] | None:
    """Send {{ tmpl.name | replace('_', ' ') }} email.{% if tmpl.description %}

    {{ tmpl.description }}{% endif %}
    """
    context = {
{% for var in tmpl.variables %}
        "{{ var }}": {{ var }},
{% endfor %}
    }
    subject = "{{ tmpl.subject }}".format(**context)
    return send_templated_email(to, "{{ tmpl.name }}", subject, context, **kwargs)


{% endfor %}

# PRISM:PROTECTED:START - Custom Email Functions
# Add your custom email functions here. This region is preserved across regeneration.
#
# Example:
# def send_order_shipped(to: str, order_id: str, tracking_url: str) -> None:
#     """Send order shipped notification."""
#     send_templated_email(
#         to,
#         template_name="order_shipped",
#         subject=f"Order #{order_id} has shipped",
#         context={"order_id": order_id, "tracking_url": tracking_url},
#     )
# PRISM:PROTECTED:END
