from datetime import datetime
from typing import List, Optional, Union

from pydantic import BaseModel, EmailStr, Field

class BaseWeixinUser(BaseModel):
    userid: Optional[str] = None
    corp_id: Optional[str] = None
    vid: Optional[str] = None
    wx_id_hash: Optional[str] = None
    is_quit: Optional[bool] = None
    crmid: Optional[int] = None
    depart_ids: Optional[List[int]] = None
    position: Optional[str] = None
    role: Optional[str] = None
    realname: Optional[str] = None
    username: Optional[str] = None
    english_name: Optional[str] = None
    name: Optional[str] = None
    # 将所有属性映射为可选属性
    alias: Optional[str] = None
    avatar: Optional[str] = None
    department: Optional[int] = None
    mobile: Optional[str] = None
    email: Optional[EmailStr] = None
    gender: Optional[int] = None
    has_external_user_permit: Optional[bool] = None
    erpid: Optional[int] = None
    erp_name: Optional[str] = None
    crmid: Optional[int] = None
    crm_name: Optional[str] = None

class WeixinUserCreate(BaseWeixinUser):
    userid: str = Field(example="julia")
    name: str = Field(example='用户姓名')
    role: str = Field(example='用户角色')


class WeixinUserUpdate(BaseModel):
    id: int
    crmid: Optional[int] = None
    erpid: Optional[int] = None
    is_quit: Optional[bool] = None


class Member(BaseModel):
    userid: Optional[str] = Field(description="用户ID")
    name: Optional[str] = Field(description="用户姓名")
    type: Optional[int] = Field(description="用户类型: 1-外部成员, 2-内部成员")
    join_time: Optional[int] = Field(description="加入时间")
    # join_scene: Optional[int] = Field(description="加入场景")
    # unionid: Optional[str] = Field(description="微信UnionID")
    # invitor: Optional[dict] = Field(description="邀请人")
    group_nickname: Optional[str] = Field(description="群内昵称")

class BaseWeixinGroupChat(BaseModel):
    chat_id: Optional[str] = Field(description="企业微信中的群聊ID")
    name: Optional[str] = Field(description="群聊名称")
    admin_list: Optional[List[Member]] = Field(description="群聊管理员列表")
    create_time: Optional[int] = Field(description="群聊创建时间")
    owner: Optional[str] = Field(description="群聊群主")
    member_version: Optional[str] = Field(description="群聊成员版本")
    external_member_list: Optional[List[Member]] = Field(description="群聊外部成员列表")
    internal_member_list: Optional[List[Member]] = Field(description="群聊内部成员列表")

class WeixinGroupChatEvent(BaseWeixinGroupChat):
    external_user_count: Optional[int] = Field(description="群聊外部成员数量")
    internal_member_count: Optional[int] = Field(description="群聊内部成员数量")

    __exclude_filter__ = ["admin_list", "member_version", "id"]


class TriggerWeixinGroupChat(BaseModel):
    group_name: str = Field(alias="groupName", description="群聊名称")
    order_id: Optional[str] = Field(alias="orderId", description="订单ID", default=None)
    user_ids: Optional[List[str]] = Field(alias="userIds", description="群聊成员列表")
    external_user_ids: Optional[List[str]] = Field(alias="externalUserIds", description="群聊外部成员列表")

class WeixinGroupChatCreate(BaseWeixinGroupChat):
    pass

class WeixinGroupChatUpdate(BaseWeixinGroupChat):
    pass

class BaseWeixinGroupChatXingyun(BaseModel):
    xingyun_chat_id: Optional[int] = Field(alias="chatId")
    avatars: Optional[List[Union[str, None]]] = Field(alias="avatars")
    chat_id: Optional[str] = Field(alias="groupChatId")
    name: Optional[str] = Field(alias="chatName")
    admin_list: Optional[List[str]] = Field(alias="admins")
    create_time: Optional[int] = Field(alias="createTime")
    owner: Optional[str] = Field(alias="ownerName")
    member_version: Optional[str] = Field(default=None)
    external_member_list: Optional[List[str]] = Field(default=None)
    internal_member_list: Optional[List[str]] = Field(default=None)

class WeixinGroupChatXingyunCreate(BaseWeixinGroupChatXingyun):
    chat_id: str = Field(alias="groupChatId")
    name: str = Field(alias="chatName")
    xingyun_chat_id: int = Field(alias="chatId")


class BaseWeixinCustomer(BaseModel):
    order_id: Optional[str] = Field(alias="orderId", description="订单ID", default=None)
    shop_name: Optional[str] = Field(alias="shopName", description="店铺名称", default=None)
    weixin_id: Optional[str] = Field(alias="weixinId", description="微信用户ID", default=None)
    weixin_name: Optional[str] = Field(alias="weixinName", description="微信用户名", default=None)
    weixin_unionid: Optional[str] = Field(alias="weixinUnionid", description="微信用户UnionID", default=None)
    weixin_avatar: Optional[str] = Field(alias="weixinAvatar", description="微信用户头像", default=None)
    xingyun_id: Optional[int] = Field(alias="xingyunId", description="星云用户ID", default=None)
    xingyun_sex: Optional[int] = Field(alias="xingyunSex", description="星云用户性别", default=None)
    xingyun_name: Optional[str] = Field(alias="xingyunName", description="星云用户名", default=None)
    xingyun_avatar: Optional[str] = Field(alias="xingyunAvatar", description="星云用户头像", default=None)
    xingyun_tags: Optional[list] = Field(alias="xingyunTags", description="星云用户标签", default=None)
    xingyun_external_userid: Optional[str] = Field(alias="xingyunExternalUserid", description="星云用户外部ID", default=None)
    xingyun_sync: Optional[bool] = Field(alias="xingyunSync", description="是否已同步到星云", default=False)
    taobao_id: Optional[str] = Field(alias="taobaoId", description="淘宝用户ID", default=None)
    taobao_name: Optional[str] = Field(alias="taobaoName", description="淘宝用户名", default=None)
    taobao_avatar: Optional[str] = Field(alias="taobaoAvatar", description="淘宝用户头像", default=None)
    need_confirm: Optional[bool] = Field(alias="needConfirm", description="是否需要确认", default=None)
    extra: Optional[dict] = Field(alias="extra", description="额外信息", default=None)

class WeixinCustomerCreate(BaseWeixinCustomer):
    pass

class WeixinCustomerUpdate(BaseWeixinCustomer):
    pass

class WeixinCustomerFilter(BaseModel):
    order_id: Optional[str] = Field(description="订单ID", default=None)
    has_order: Optional[bool] = Field(description="是否有订单", default=None)
    shop_name: Optional[str] = Field(description="店铺名称", default=None)
    # weixin_id: Optional[str] = Field(description="微信用户ID", default=None)
    # weixin_unionid: Optional[str] = Field(description="微信用户UnionID", default=None)
    # xingyun_id: Optional[int] = Field(description="星云用户ID", default=None)
    # xingyun_name: Optional[str] = Field(description="星云用户名", default=None)
    xingyun_tags: Optional[str] = Field(description="星云用户标签", default=None)
    xingyun_sync: Optional[bool] = Field(description="是否已同步到星云", default=False)
    # taobao_id: Optional[str] = Field(description="淘宝用户ID", default=None)
    # taobao_name: Optional[str] = Field(description="淘宝用户名", default=None)
    union_id: Optional[str] = Field(description="联合用户ID", default=None)
    union_name: Optional[str] = Field(description="联合用户名", default=None)


class BaseWeixinCustomerXingyun(BaseModel):
    weixin_id: Optional[str] = Field(alias="external_userid", description="微信用户ID", default=None)
    weixin_name: Optional[str] = Field(alias="weixin_name", description="微信用户名", default=None)
    xingyun_id: Optional[int] = Field(alias="cid", description="星云用户ID")
    xingyun_name: Optional[str] = Field(alias="name", description="星云用户名")
    xingyun_avatar: Optional[str] = Field(alias="avatar", description="星云用户头像")
    xingyun_external_userid: Optional[str] = Field(alias="externalUserid", description="星云用户外部ID")
    xingyun_sex: Optional[int] = Field(alias="sex", description="星云用户性别", default=None)
    xingyun_tags: Optional[list] = Field(alias="tags", description="星云用户标签", default=None)
    # 移除这些字段，因为它们将被存储在 extra 中
    exclusive_member_info: Optional[dict] = Field(alias="exclusiveMemberInfo", description=" exclusiveMemberInfo", default=None)
    extra: Optional[dict] = Field(alias="extra", description="额外信息", default=None)

    def transform_to_extra(self):
        new_obj = self.model_dump(exclude_unset=True, by_alias=True)
        extra = {
            'exclusive_member_info': new_obj.pop('exclusiveMemberInfo', None),
        }
        new_name = new_obj.get('name', None)
        if new_name: new_obj['weixin_name'] = new_name
        return self.__class__(**new_obj, extra=extra)
        
class WeixinCustomerXingyunCreate(BaseWeixinCustomerXingyun):
    pass


class WeixinUserBindInfo(BaseModel):
    userid: str = Field(description="微信用户ID")
    username: Optional[str] = Field(description="微信用户名", alias="name")
    role: Optional[str] = Field(description="用户角色", default=None)
    erp_id: Optional[int] = Field(description="ERP用户ID", default=None)
    avatar: Optional[str] = Field(description="微信用户头像", default=None)
    taobao_id: Optional[str] = None


class WeixinOrderBindInfo(BaseModel):
    userid: str = Field(description="微信用户ID")
    order_id: str = Field(description="订单ID")
    remark: Optional[str] = Field(description="企业微信向ERP同步订单时的备注", default=None)

class WeixinOrderRemark(BaseModel):
    order_id: str = Field(description="订单ID")
    remark: Optional[str] = Field(description="企业微信向ERP同步订单时的备注", default=None)


class OrderUser(BaseModel):
    role: Optional[str] = Field(description="用户角色", default=None)
    taobao_id: Optional[str] = Field(description="旺旺名称", alias="id", default=None)
    taobao_name: Optional[str] = Field(description="旺旺名称", alias="name", default=None)
    userid: Optional[str] = Field(description="微信用户ID", default=None)

class BindOrderResultEvent(BaseModel):
    shop_name: Optional[str] = Field(description="店铺名称")
    price: Optional[int] = Field(description="订单金额")
    discount_fee: Optional[int] = Field(description="折扣金额")
    real_payment: Optional[int] = Field(description="实付金额")
    remark: Optional[str] = Field(description="企业微信向ERP同步订单时的备注", default=None)
    status_string: Optional[str] = Field(description="订单状态")
    pay_time: Optional[str] = Field(description="支付时间")
    users: Optional[List[OrderUser]] = Field(description="绑定的用户信息")

    # __exclude_filter__ = ["member_version", "id"]


class CustomerRepeatPurchaseEvent(BindOrderResultEvent):
    has_service_group: Optional[bool] = Field(description="是否有服务群", default=None)
    __exclude_filter__ = ["remark", "status_string", "pay_time"]

# CUSTOMER_SERVICE_ASSIGN_ORDER
class CustomerAssignOrderEvent(BindOrderResultEvent):
    __exclude_filter__ = ["remark", "status_string"]

# CUSTOMER_SERVICE_ASSIGN_ORDER
class CustomerRefundOrderEvent(BindOrderResultEvent):
    __exclude_filter__ = ["status_string"]

