# from django.db.models import F
# from django.db.models.signals import post_save
# from django.dispatch import receiver
#
# from core.apps.eggs.models import Order
#
#
# @receiver(post_save, sender=Order)
# def update_order_status(sender, instance, **kwargs):
#     if instance.courier_id is not None and instance.status == "pending":
#         instance.status = "delivery"
#         instance.save()
#     if (
#             instance.debt == 0
#             and instance.price == instance.price_paid
#             and instance.status in ["success"]
#     ):
#         instance.status = "done"
#         instance.save()
#     if instance.status == "success":
#         order_items = instance.order_items.all()
#         for item in order_items:
#             if item.courier_product_id:
#                 item.courier_product_id.count = F("count") - item.count
#                 item.courier_product_id.save()
#
#         if instance.market_id:
#             market_instance = instance.market_id
#             market_instance.refresh_from_db()
#             market_instance.debt_paid = F("debt_paid") + instance.price_paid
#             market_instance.debt_unpaid = (
#                     F("debt_unpaid") + instance.debt - instance.price_paid
#             )
#             market_instance.save(update_fields=["debt_paid", "debt_unpaid"])
#
#             Order.objects.filter(pk=instance.pk).update(
#                 debt=instance.price - instance.price_paid
#             )
#
#     if instance.status == "cancel":
#         if instance.market_id:
#             market_instance = instance.market_id
#             market_instance.refresh_from_db()
#             market_instance.debt_paid = F("debt_paid") - instance.price_paid
#             market_instance.debt_unpaid = (
#                     F("debt_unpaid") - instance.debt + instance.price_paid
#             )
#             market_instance.save(update_fields=["debt_paid", "debt_unpaid"])
#             Order.objects.filter(pk=instance.pk).update(debt=0)
#         order_items = instance.order_items.all()
#         for item in order_items:
#             if item.courier_product_id:
#                 item.courier_product_id.count = F("count") + item.count
#                 item.courier_product_id.save()
#     if instance.status == "cancel" and instance.debt > 0:
#         Order.objects.filter(pk=instance.pk).update(price_paid=0)
#         if instance.market_id:
#             market_instance = instance.market_id
#             market_instance.refresh_from_db()
#             market_instance.debt_paid = F("debt_paid") - instance.price_paid
#             market_instance.debt_unpaid = (
#                     F("debt_unpaid") - instance.debt + instance.price_paid
#             )
#             market_instance.save(update_fields=["debt_paid", "debt_unpaid"])
#         Order.objects.filter(pk=instance.pk).update(debt=0)
#     order_items = instance.order_items.all()
#     for item in order_items:
#         if item.courier_product_id:
#             item.courier_product_id.count = F("count") + item.count
#             item.courier_product_id.save()
#
#     if instance.status == "cancel" and instance.debt == 0:
#         Order.objects.filter(pk=instance.pk).update(price_paid=0)
#         if instance.market_id:
#             market_instance = instance.market_id
#             market_instance.refresh_from_db()
#             market_instance.debt_paid = F("debt_paid") - instance.price_paid
#             market_instance.debt_unpaid = (
#                     F("debt_unpaid") - instance.debt + instance.price_paid
#             )
#             market_instance.save(update_fields=["debt_paid", "debt_unpaid"])
#     order_items = instance.order_items.all()
#     for item in order_items:
#         if item.courier_product_id:
#             item.courier_product_id.count = F("count") + item.count
#             item.courier_product_id.save()


# @receiver(post_save, sender=Order)
# def update_order_status(sender, instance, created, **kwargs):
#     if created:
#         if instance.courier_id is None:
#             instance.status = "pending"
#         elif instance.courier_id is not None and instance.status == "pending":
#             instance.status = "delivery"
#         instance.save()
#     else:
#         if instance.status == "success":
#             order_items = instance.order_items.all()
#             for item in order_items:
#                 if item.courier_product_id:
#                     if item.courier_product_id.count < item.count:
#                         raise ValueError(
#                             "Count cannot be greater than courier product count"
#                         )
#                     item.courier_product_id.count -= item.count
#                     item.courier_product_id.save()
#
#         if instance.price == instance.price_paid and instance.status == "success":
#             instance.status = "done"
#             instance.save()
