from account.tasks import send_email_with_context
from celery import shared_task

from django.apps import apps
from django.utils import timezone


# -- CLEANUP TASKS --

# Delete expired/cancelled orders (older than 24 hours)
@shared_task
def delete_expired_orders():
    Order = apps.get_model('commerce', 'Order')

    expired_orders = Order.objects.filter(status=Order.OrderStatus.CANCELLED, created_at__lt=timezone.now() - timezone.timedelta(hours=24))
    count = expired_orders.count()
    expired_orders.delete()
    return count


# -- NOTIFICATIONS CARRIER --

# Zásilkovna
@shared_task
def notify_zasilkovna_sended(order = None, **kwargs):
    if not order:
        raise ValueError("Order must be provided for notification.")
    
    if kwargs:
        print("Additional kwargs received in notify_zasilkovna_sended:", kwargs)
    
    send_email_with_context(
        recipients=order.email,
        subject="Your order has been shipped",
        template_path="email/shipping/zasilkovna/zasilkovna_sended.html",
        context={
            "order": order,
        })


# Shop
@shared_task
def notify_Ready_to_pickup(order = None, **kwargs):
    if not order:
        raise ValueError("Order must be provided for notification.")
    
    if kwargs:
        print("Additional kwargs received in notify_Ready_to_pickup:", kwargs)
    
    send_email_with_context(
        recipients=order.email,
        subject="Your order is ready for pickup",
        template_path="email/shipping/ready_to_pickup/ready_to_pickup.html",
        context={
            "order": order,
        })


# -- NOTIFICATIONS ORDER --

@shared_task
def notify_order_successfuly_created(order = None, **kwargs):
    if not order:
        raise ValueError("Order must be provided for notification.")
    
    if kwargs:
        print("Additional kwargs received in notify_order_successfuly_created:", kwargs)
    
    send_email_with_context(
        recipients=order.email,
        subject="Your order has been successfully created",
        template_path="email/order_created.html",
        context={
            "order": order,
        })


@shared_task
def notify_order_payed(order = None, **kwargs):
    if not order:
        raise ValueError("Order must be provided for notification.")
    
    if kwargs:
        print("Additional kwargs received in notify_order_payed:", kwargs)
    
    send_email_with_context(
        recipients=order.email,
        subject="Your order has been paid",
        template_path="email/order_paid.html",
        context={
            "order": order,
        })


@shared_task
def notify_about_missing_payment(order = None, **kwargs):
    if not order:
        raise ValueError("Order must be provided for notification.")
    
    if kwargs:
        print("Additional kwargs received in notify_about_missing_payment:", kwargs)
    
    send_email_with_context(
        recipients=order.email,
        subject="Payment missing for your order",
        template_path="email/order_missing_payment.html",
        context={
            "order": order,
        })


# -- NOTIFICATIONS REFUND --

@shared_task
def notify_refund_items_arrived(order = None, **kwargs):
    if not order:
        raise ValueError("Order must be provided for notification.")
    
    if kwargs:
        print("Additional kwargs received in notify_refund_items_arrived:", kwargs)
    
    send_email_with_context(
        recipients=order.email,
        subject="Your refund items have arrived",
        template_path="email/order_refund_items_arrived.html",
        context={
            "order": order,
        })


# Refund accepted, returning money
@shared_task
def notify_refund_accepted(order = None, **kwargs):
    if not order:
        raise ValueError("Order must be provided for notification.")
    
    if kwargs:
        print("Additional kwargs received in notify_refund_accepted:", kwargs)
    
    send_email_with_context(
        recipients=order.email,
        subject="Your refund has been accepted",
        template_path="email/order_refund_accepted.html",
        context={
            "order": order,
        })


# -- NOTIFICATIONS ORDER STATUS --

@shared_task
def notify_order_cancelled(order = None, **kwargs):
    if not order:
        raise ValueError("Order must be provided for notification.")
    
    if kwargs:
        print("Additional kwargs received in notify_order_cancelled:", kwargs)
    
    send_email_with_context(
        recipients=order.email,
        subject="Your order has been cancelled",
        template_path="email/order_cancelled.html",
        context={
            "order": order,
        })


@shared_task
def notify_order_completed(order = None, **kwargs):
    if not order:
        raise ValueError("Order must be provided for notification.")
    
    if kwargs:
        print("Additional kwargs received in notify_order_completed:", kwargs)
    
    send_email_with_context(
        recipients=order.email,
        subject="Your order has been completed",
        template_path="email/order_completed.html",
        context={
            "order": order,
        })
