from tortoise import fields

from .base import BaseModel, TimestampMixin
from .enums import MsgType, ActionType

class Msg(BaseModel, TimestampMixin):
    hash_id = fields.CharField(max_length=64, null=True, description="消息哈希ID", index=True, unique=True)
    title = fields.CharField(max_length=64, null=True, description="消息标题", index=True)
    content = fields.TextField(null=True, description="消息内容")
    detail = fields.JSONField(default={}, description="消息详情")
    is_send = fields.BooleanField(default=False, description="是否已发送", index=True)
    send_at = fields.DatetimeField(null=True, description="发送时间")
    is_read = fields.BooleanField(default=False, description="是否已读", index=True)
    read_at = fields.DatetimeField(null=True, description="已读时间")
    is_delete = fields.BooleanField(default=False, description="是否已删除", index=True)
    delete_at = fields.DatetimeField(null=True, description="删除时间")
    type = fields.CharEnumField(MsgType, null=True, description="消息类型", index=True)
    owner_id = fields.CharField(max_length=64, null=True, description="消息所有者ID", index=True)
    owner_name = fields.CharField(max_length=64, null=True, description="消息所有者名称", index=True)

    class Meta:
        table = "system_msg"


class Follow(BaseModel, TimestampMixin):
    order_id = fields.CharField(max_length=64, null=True, description="订单ID", index=True)
    order_title = fields.TextField(null=True, description="订单标题")
    order_pic = fields.CharField(max_length=256, null=True, description="订单图片")
    pay_time = fields.DatetimeField(null=True, description="支付时间", index=True)
    pay_price = fields.FloatField(null=True, description="支付金额", index=True)
    remark = fields.TextField(null=True, description="erp额外备注")

    customer_id = fields.CharField(max_length=64, null=True, description="客户ID", index=True)
    customer_name = fields.CharField(max_length=64, null=True, description="客户名称", index=True)
    customer_taobao_id = fields.CharField(max_length=64, null=True, description="客户淘宝ID", index=True)

    staff_id = fields.CharField(max_length=64, null=True, description="企微员工ID", index=True)
    staff_name = fields.CharField(max_length=64, null=True, description="企微员工名称", index=True)
    staff_time = fields.DatetimeField(null=True, description="添加时间", index=True)

    designer_id = fields.CharField(max_length=64, null=True, description="设计师ID", index=True)
    designer_name = fields.CharField(max_length=64, null=True, description="设计师名称", index=True)

    shop_name = fields.CharField(max_length=64, null=True, description="店铺名称", index=True)
    kefu_id = fields.CharField(max_length=64, null=True, description="淘宝客服ID", index=True)
    kefu_name = fields.CharField(max_length=64, null=True, description="淘宝客服名称", index=True)
    
    is_delete = fields.BooleanField(default=False, description="是否已删除", index=True)

    feishu_record_id = fields.CharField(max_length=64, null=True, description="飞书记录ID", index=True)
    is_add_to_feishu = fields.BooleanField(default=False, description="是否已添加到飞书", index=True)
    is_update_to_feishu = fields.BooleanField(default=False, description="是否已更新到飞书", index=True)
    is_delete_from_feishu = fields.BooleanField(default=False, description="是否已从飞书删除", index=True)

    class Meta:
        table = "system_follow"

    def to_feishu_record(self, fields: dict=None):
        fields = fields or {}
        return {
            "record_id": self.feishu_record_id,
            "fields": {
                "付款时间": self.pay_time.strftime("%Y-%m-%d %H:%M:%S"),
                "店铺名": self.shop_name,
                "成交客服": self.kefu_name,
                "旺旺id": self.customer_name,
                "添加企微客服": self.staff_name,
                "订单编号": self.order_id,
                "设计师": self.designer_name,
                **fields 
            }
        }
