import html2text
from django.conf import settings
from django.core import mail
from django.template.loader import render_to_string


def send_order_confirmation_mail(order):
    template_name = "mail/order_confirm_mail.html"

    subject = (
        f"[{settings.PROJECT_NAME}] Votre commande pour « {order.grouped_order.name} »"
    )
    html_message = render_to_string(
        template_name, {"order": order, "base_url": settings.BASE_URL}
    )

    # create plain message text from html
    plain_message = html2text.html2text(html_message)
    # do not render the logo link
    plain_message = plain_message[plain_message.rfind("Merci,") :]

    from_email = settings.DEFAULT_FROM_EMAIL
    to = order.author.email

    mail.send_mail(subject, plain_message, from_email, [to], html_message=html_message)
