from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver
from .models import Order
from .sms_utils import notify_admins_order_paid, notify_user_order_paid, notify_user_order_shipped

@receiver(pre_save, sender=Order)
def track_order_changes(sender, instance, **kwargs):
    """
    在保存之前检查状态变化
    """
    if instance.pk:
        try:
            old_instance = Order.objects.get(pk=instance.pk)
            
            # 检查是否从非支付状态变为支付状态
            if old_instance.status != 'paid' and instance.status == 'paid':
                instance._was_paid = True
            
            # 检查是否发货 (状态变为 shipped 且有单号)
            # 或者已经是 shipped 状态但刚填入单号
            if instance.status == 'shipped' and instance.tracking_number:
                if old_instance.status != 'shipped' or not old_instance.tracking_number:
                    instance._was_shipped = True
                    
        except Order.DoesNotExist:
            pass

@receiver(post_save, sender=Order)
def send_order_notifications(sender, instance, created, **kwargs):
    """
    在保存之后发送通知
    """
    if created:
        return

    # 1. 处理支付成功通知
    if getattr(instance, '_was_paid', False):
        try:
            # 只有当订单不是活动订单时才发送普通支付成功短信
            # 活动订单会在 views.py 中单独处理（发送报名成功短信）
            if not (hasattr(instance, 'activity') and instance.activity):
                print(f"订单 {instance.id} 支付成功，触发短信通知流程...")
                notify_admins_order_paid(instance)
                notify_user_order_paid(instance)
            else:
                print(f"订单 {instance.id} 是活动订单，跳过普通支付短信通知（已在 views.py 处理）")
            
            # 清除标记防止重复发送 (虽然实例通常是新的，但保险起见)
            instance._was_paid = False
        except Exception as e:
            print(f"发送支付成功短信失败: {str(e)}")

    # 2. 处理发货通知
    if getattr(instance, '_was_shipped', False):
        try:
            # 同样，活动订单不需要发送发货短信（通常活动无需发货）
            if not (hasattr(instance, 'activity') and instance.activity):
                print(f"订单 {instance.id} 已发货，触发短信通知流程...")
                notify_user_order_shipped(instance)
            else:
                print(f"订单 {instance.id} 是活动订单，跳过发货短信通知")
                
            instance._was_shipped = False
        except Exception as e:
            print(f"发送发货短信失败: {str(e)}")
