from pydantic import BaseModel, EmailStr
from typing import Optional, List, Any
from datetime import datetime

class UserBase(BaseModel):
    username: str
    email: EmailStr

class UserCreate(UserBase):
    password: str

class UserUpdate(BaseModel):
    email: Optional[EmailStr] = None
    password: Optional[str] = None

class UserResponse(UserBase):
    id: int
    is_active: bool
    is_admin: bool

    class Config:
        from_attributes = True

class Token(BaseModel):
    access_token: str
    token_type: str

class TokenData(BaseModel):
    username: Optional[str] = None

class ChatRoomBase(BaseModel):
    name: str

class ChatRoomCreate(ChatRoomBase):
    member_ids: List[int] = []  # 添加成员 ID 列表

class ChatRoomResponse(ChatRoomBase):
    id: int
    owner_id: int
    share_link: Optional[str]
    is_private: bool

    class Config:
        from_attributes = True

class MessageResponse(BaseModel):
    id: int
    room_id: int
    sender_id: int
    sender_name: Optional[str] = None  # 添加发送者用户名
    content: str
    timestamp: datetime
    is_sensitive: bool
    
    class Config:
        from_attributes = True

class FriendResponse(BaseModel):
    id: int
    friend_id: int
    alias: Optional[str]
    friend: Optional[UserResponse] = None

    class Config:
        from_attributes = True

class AIModelBase(BaseModel):
    name: str
    api_key: str
    base_url: str

class AIModelCreate(AIModelBase):
    pass

class AIModelResponse(AIModelBase):
    id: int
    is_default: bool = False  # 是否为默认模型
    
    class Config:
        from_attributes = True

class AIToolBase(BaseModel):
    name: str
    description: str
    parameters_schema: Any
    function_code: Optional[str] = None
    is_code_tool: bool = False

class AIToolCreate(AIToolBase):
    pass

class AIToolResponse(AIToolBase):
    id: int
    
    class Config:
        from_attributes = True

class DigitalEmployeeBase(BaseModel):
    name: str
    model_name: str  # 使用模型名称而不是ID
    system_prompt: str
    tool_ids: List[int]

class DigitalEmployeeCreate(DigitalEmployeeBase):
    pass

class DigitalEmployeeResponse(DigitalEmployeeBase):
    id: int
    model_id: int  # 保留model_id用于内部查询
    
    class Config:
        from_attributes = True
    class Config:
        from_attributes = True
