"""
AI 应用 Schema
"""
from typing import Optional, List, Any
from datetime import datetime

from pydantic import BaseModel, Field, ConfigDict

from app.base_schema import CSTDatetime


class AppCreate(BaseModel):
    """创建应用"""
    model_config = ConfigDict(protected_namespaces=())
    
    name: str = Field(..., max_length=100, description="应用名称")
    code: str = Field(..., max_length=100, description="应用编码")
    description: str = Field(default="", description="描述")
    icon: str = Field(default="", description="图标")
    app_type: str = Field(default="chat", description="应用类型")
    model_id: Optional[str] = Field(None, description="默认模型 ID")
    system_prompt: str = Field(default="", description="系统提示词")
    temperature: float = Field(default=0.7, description="温度")
    top_p: float = Field(default=1.0, description="top_p")
    max_tokens: int = Field(default=2048, description="最大 Token")
    opening_statement: str = Field(default="", description="开场白")
    suggested_questions: List[str] = Field(default_factory=list, description="建议问题")
    is_public: bool = Field(default=False, description="是否公开")


class AppUpdate(BaseModel):
    """更新应用"""
    model_config = ConfigDict(protected_namespaces=())
    
    name: Optional[str] = None
    description: Optional[str] = None
    icon: Optional[str] = None
    model_id: Optional[str] = None
    system_prompt: Optional[str] = None
    temperature: Optional[float] = None
    top_p: Optional[float] = None
    max_tokens: Optional[int] = None
    opening_statement: Optional[str] = None
    suggested_questions: Optional[List[str]] = None
    is_public: Optional[bool] = None
    workflow_definition: Optional[dict] = None


class AppResponse(BaseModel):
    """应用输出"""
    model_config = ConfigDict(from_attributes=True, protected_namespaces=())
    
    id: str
    name: str
    code: str
    description: str = ""
    icon: str = ""
    app_type: str = "chat"
    status: str = "draft"
    model_id: Optional[str] = None
    model_name: str = ""
    system_prompt: str = ""
    temperature: float = 0.7
    top_p: float = 1.0
    max_tokens: int = 2048
    opening_statement: str = ""
    suggested_questions: List[Any] = Field(default_factory=list)
    workflow_definition: dict = Field(default_factory=dict)
    is_public: bool = False
    conversation_count: int = 0
    message_count: int = 0
    sort: int = 0
    sys_create_datetime: Optional[CSTDatetime] = None
    sys_update_datetime: Optional[CSTDatetime] = None


class AppListResponse(BaseModel):
    """应用列表输出"""
    model_config = ConfigDict(from_attributes=True, protected_namespaces=())
    
    id: str
    name: str
    code: str
    description: str = ""
    icon: str = ""
    app_type: str = "chat"
    status: str = "draft"
    model_name: str = ""
    is_public: bool = False
    conversation_count: int = 0
    message_count: int = 0
    sys_create_datetime: Optional[CSTDatetime] = None
