from pathlib import Path

from fastapi_mail import ConnectionConfig, FastMail, MessageSchema, MessageType
from rvpc.settings import mail_settings


CONF = ConnectionConfig(
    MAIL_USERNAME=mail_settings.user.get_secret_value(),
    MAIL_PASSWORD=mail_settings.password.get_secret_value(),
    MAIL_FROM=mail_settings.user.get_secret_value(),
    MAIL_PORT=465,
    MAIL_SERVER="ssl0.ovh.net",
    MAIL_FROM_NAME=mail_settings.name.get_secret_value(),
    USE_CREDENTIALS=True,
    MAIL_SSL_TLS=True,
    MAIL_STARTTLS=False,
    VALIDATE_CERTS=True,
    TEMPLATE_FOLDER=Path(__file__).parent.parent / "templates",
)


async def notify(template_body: dict, recipient: str):
    message = MessageSchema(
        subject="Fastapi mail module",
        recipients=[recipient],
        template_body=template_body,
        subtype=MessageType.html,
    )

    fm = FastMail(CONF)
    await fm.send_message(message, template_name="emails/notification.html")
