import logging
from django.core.mail import send_mail
from django.conf import settings
from django.utils.translation import gettext_lazy as _

logger = logging.getLogger(__name__)

def send_otp_email(email, code):
    """
    Sends an OTP code via email.
    """
    subject = _("Your verification code for MASAR CARGO")
    message = _("Your verification code for MASAR CARGO is: %(code)s") % {"code": code}
    from_email = settings.DEFAULT_FROM_EMAIL
    recipient_list = [email]

    try:
        send_mail(subject, message, from_email, recipient_list)
        logger.info(f"OTP email sent to {email}")
        return True
    except Exception as e:
        logger.exception(f"Exception while sending OTP email: {str(e)}")
        return False

def send_contact_message(name, email, message):
    """
    Sends a contact message to the admin.
    """
    subject = _("New contact message from %(name)s") % {"name": name}
    full_message = f"Name: {name}\nEmail: {email}\n\nMessage:\n{message}"
    from_email = settings.DEFAULT_FROM_EMAIL
    recipient_list = settings.CONTACT_EMAIL_TO

    try:
        send_mail(subject, full_message, from_email, recipient_list)
        logger.info(f"Contact message from {email} sent to admins")
        return True
    except Exception as e:
        logger.exception(f"Exception while sending contact message: {str(e)}")
        return False
