from typing import Optional
from datetime import datetime

from pydantic import BaseModel, Field
from app.models.enums import MsgType, ActionType


class BaseMsg(BaseModel):
    hash_id: Optional[str] = Field(description="消息哈希ID")
    title: Optional[str] = Field(description="消息标题")
    content: Optional[str] = Field(description="消息内容")
    detail: Optional[str] = Field(default={}, description="消息详情")
    is_read: Optional[bool] = Field(default=False, description="是否已读")
    read_at: Optional[datetime] = Field(default=None, description="已读时间")
    is_delete: Optional[bool] = Field(default=False, description="是否已删除")
    delete_at: Optional[datetime] = Field(default=None, description="删除时间")
    is_send: Optional[bool] = Field(default=False, description="是否已发送")
    send_at: Optional[datetime] = Field(default=None, description="发送时间")
    type: Optional[MsgType] = Field(default=MsgType.SYSTEM, description="消息类型")
    owner_id: Optional[str] = Field(description="消息所有者ID")
    owner_name: Optional[str] = Field(description="消息所有者名称")
    

class MsgCreate(BaseMsg):
    hash_id: str = Field(description="消息哈希ID")
    title: str = Field(description="消息标题")
    content: str = Field(description="消息内容")
    type: MsgType = Field(default=MsgType.SYSTEM, description="消息类型")
    owner_id: str = Field(description="消息所有者ID")
    owner_name: str = Field(description="消息所有者名称")


class MsgUpdate(BaseModel):
    id: int
    is_read: Optional[bool] = Field(default=None, description="是否已读")
    is_delete: Optional[bool] = Field(default=None, description="是否已删除")
    is_send: Optional[bool] = Field(default=None, description="是否已发送")
    
class MsgNewOrder(BaseModel):
    type: Optional[MsgType] = Field(default=MsgType.NEW_ORDER, description="消息类型")
    order_id: str = Field(description="订单ID")
    order_status: str = Field(description="订单状态")
    is_refund: Optional[bool] = Field(default=False, alias="order_type", description="是否退款: 0-普通订单，1-退款订单")
    buyer_id: str = Field(description="买家ID")
    buyer_nick: str = Field(description="买家名称")

    def __str__(self):
        return f'订单：{self.order_id} {self.order_status}，买家：{self.buyer_nick or self.buyer_id} {"退款" if self.is_refund else ""}'

class MsgFilter(BaseModel):
    type: Optional[MsgType] = Field(default=None, description="消息类型")
    is_read: Optional[bool] = Field(default=None, description="是否已读")
    is_refund: Optional[bool] = Field(default=None, description="是否退款订单")

class BaseAction(BaseModel):
    type: Optional[ActionType] = Field(default=None, description="操作类型")
    detail: Optional[dict] = Field(default={}, description="操作详情")
    user_id: Optional[str] = Field(default=None, description="操作用户ID")
    user_name: Optional[str] = Field(default=None, description="操作用户名称")

class ActionCreate(BaseAction):
    done: Optional[bool] = Field(default=False, description="是否完成")
    type: ActionType = Field(description="操作类型")
    detail: dict = Field(description="操作详情")
    userid: str = Field(description="操作用户ID")
    username: str = Field(description="操作用户名称")

class ActionUpdate(BaseAction):
    id: int = Field(description="操作ID")

class FollowFormErp(BaseModel):
    order_id: str = Field(description="订单ID", alias="trade_no")
    order_title: str = Field(description="订单标题", alias="title")
    pay_time: str = Field(description="支付时间", alias="pay_time")
    pay_price: str = Field(description="支付金额", alias="price")
    remark: str = Field(description="erp额外备注", alias="remark")

    shop_name: str = Field(description="店铺名称", alias="shop_name")

    """ 
    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)

    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)
    
    """

class FeishuEvent(BaseModel):
    challenge: str = Field(description="验证challenge")
    
