"""联系人域模型：联系人 / 联系人列表 / 群聊列表 / 群成员。"""

from __future__ import annotations

from typing import Optional

from pydantic import BaseModel, Field


# ---------------------------------------------------------------------------
# 联系人接口
# ---------------------------------------------------------------------------
class Contact(BaseModel):
    """联系人。"""

    wxid: str
    nickname: str = ""
    remark: str = ""
    avatar_url: str = ""
    type: str = Field(
        default="person",
        description="person / group / official / system",
    )

    # 扩展字段：从 contact.db 的 contact 表读取
    alias: Optional[str] = Field(default=None, description="微信号/别名")
    encrypt_username: Optional[str] = Field(default=None, description="加密用户名")
    quan_pin: Optional[str] = Field(default=None, description="全拼")
    pin_yin_initial: Optional[str] = Field(default=None, description="拼音首字母")
    big_head_url: Optional[str] = Field(default=None, description="高清头像 URL")
    small_head_url: Optional[str] = Field(default=None, description="缩略头像 URL")
    description: Optional[str] = Field(default=None, description="个性签名/描述")
    local_type: Optional[int] = Field(default=None, description="联系人类型标记")
    verify_flag: Optional[int] = Field(default=None, description="认证标记")
    delete_flag: Optional[int] = Field(default=None, description="删除标记")
    chat_room_type: Optional[int] = Field(default=None, description="群类型标记")


class ContactsResponse(BaseModel):
    """联系人列表响应。"""

    contacts: list[Contact] = Field(default_factory=list)
    total: int = 0


class GroupsResponse(BaseModel):
    """群聊列表响应。"""

    groups: list[Contact] = Field(default_factory=list)
    total: int = 0


# ---------------------------------------------------------------------------
# 群成员接口
# ---------------------------------------------------------------------------
class GroupMember(BaseModel):
    """群成员。"""

    wxid: str
    nickname: str = ""
    display_name: str = ""
    is_admin: bool = False


class GroupMembersResponse(BaseModel):
    """群成员列表响应。"""

    group_wxid: str
    members: list[GroupMember] = Field(default_factory=list)
    total: int = 0


# ---------------------------------------------------------------------------
# 修改好友备注（experimental）
# ---------------------------------------------------------------------------
class SetRemarkRequest(BaseModel):
    """修改好友备注请求（experimental）。"""

    remark: str = Field(description="新备注名")
    display_name: Optional[str] = Field(
        default=None,
        description="会话显示名（备注/昵称）用于搜索框定位；"
        "不传则从 contact.db 解析（修改前的 remark 优先，其次 nickname，回退 wxid）",
    )


class SetRemarkResponse(BaseModel):
    """修改好友备注响应。"""

    success: bool = Field(default=False)
    error: Optional[str] = Field(default=None, description="失败时的错误描述")


# ---------------------------------------------------------------------------
# 添加好友（experimental）
# ---------------------------------------------------------------------------
class AddFriendRequest(BaseModel):
    """添加好友请求（experimental）。"""

    keyword: str = Field(description="搜索关键词（wxid/手机号/微信号）")
    message: str = Field(default="", description="可选好友验证消息")


class AddFriendResponse(BaseModel):
    """添加好友响应。"""

    success: bool = Field(default=False)
    error: Optional[str] = Field(default=None, description="失败时的错误描述")
