from celery import shared_task
from celery.utils.log import get_task_logger
from django.conf import settings
from django.core.mail import EmailMessage, send_mail

from templated_email import send_templated_mail

from .models import Order

logger = get_task_logger(__name__)


CONFIRM_ORDER_TEMPLATE = 'storefront/order_confirmation'
SHIP_ORDER_TEMPLATE = 'storefront/order_shipped'
ORDER_CANCEl_TEMPLATE = 'storefront/order_cancel'
ORDER_REFUND_TEMPLATE = 'storefront/order_refund'

@shared_task(name='send_order_confirmation_email')
def send_order_confirmation_email(order):
    send_templated_mail(
        template_name=CONFIRM_ORDER_TEMPLATE,
        from_email=settings.ORDER_FROM_EMAIL,
        recipient_list=[order['email']],
        context=order
    )

    logger.info(f"Order confirmation email sent to {order['email']}")

@shared_task(name='send_order_shipped_email')
def send_order_shipped_email(data):
    send_templated_mail(
        template_name=SHIP_ORDER_TEMPLATE,
        from_email=settings.ORDER_FROM_EMAIL,
        recipient_list=[data['email']],
        context=data
    )

    logger.info(f"Order shipped email sent to {data['email']}")
