"""Email service using Resend API.

⚠️ AUTO-GENERATED BY PRISM - DO NOT EDIT
"""

from __future__ import annotations

import logging

import resend

from {{ project_name }}.auth.config import auth_settings

logger = logging.getLogger(__name__)


def _configure_resend() -> None:
    """Set the Resend API key."""
    resend.api_key = auth_settings.resend_api_key


def _send_email(to: str, subject: str, html: str, text: str) -> None:
    """Send an email via Resend. Logs errors but does not raise."""
    _configure_resend()
    try:
        resend.Emails.send(
            {
                "from": auth_settings.email_from,
                "to": [to],
                "subject": subject,
                "html": html,
                "text": text,
            }
        )
    except Exception:
        logger.exception("Failed to send email to %s", to)

{% if email_verification %}
def send_verification_email(to: str, token: str) -> None:
    """Send email verification link."""
    link = f"{auth_settings.frontend_url}/verify-email?token={token}"
    html = f"""
    <h2>Verify your email</h2>
    <p>Click the link below to verify your email address and activate your account.</p>
    <p><a href="{link}">Verify Email</a></p>
    <p>This link expires in {{ email_verification_token_hours }} hours.</p>
    <p>If you didn't create an account, you can ignore this email.</p>
    """
    text = f"Verify your email: {link}\n\nThis link expires in {{ email_verification_token_hours }} hours."
    _send_email(to, "Verify your email - {{ project_title }}", html, text)
{% endif %}

{% if password_reset %}
def send_password_reset_email(to: str, token: str) -> None:
    """Send password reset link."""
    link = f"{auth_settings.frontend_url}/reset-password?token={token}"
    html = f"""
    <h2>Reset your password</h2>
    <p>Click the link below to reset your password.</p>
    <p><a href="{link}">Reset Password</a></p>
    <p>This link expires in {{ password_reset_token_hours }} hour(s).</p>
    <p>If you didn't request this, you can ignore this email.</p>
    """
    text = f"Reset your password: {link}\n\nThis link expires in {{ password_reset_token_hours }} hour(s)."
    _send_email(to, "Reset your password - {{ project_title }}", html, text)


def send_password_changed_notification(to: str) -> None:
    """Send notification that password was changed."""
    html = """
    <h2>Password changed</h2>
    <p>Your password was successfully changed.</p>
    <p>If you didn't make this change, please reset your password immediately or contact support.</p>
    """
    text = "Your password was changed. If you didn't make this change, please reset your password immediately."
    _send_email(to, "Password changed - {{ project_title }}", html, text)
{% endif %}
