from pydantic import BaseModel, Field
from typing import Optional, List, Dict, Any
from datetime import datetime


class AlgorithmBase(BaseModel):
    """算法基础模式"""
    id: Optional[str] = None
    name: str = Field(..., description="算法名称")
    description: str = Field(..., description="算法描述")
    type: str = Field(..., description="算法类型")


class AlgorithmCreate(AlgorithmBase):
    """创建算法模式"""
    # 版本相关字段
    version: str = Field(default="1.0.0", description="版本号")
    url: str = Field(default="", description="算法API地址")
    params: Dict[str, Any] = Field(default_factory=dict, description="算法参数配置")
    input_schema: Dict[str, Any] = Field(default_factory=dict, description="输入数据格式")
    output_schema: Dict[str, Any] = Field(default_factory=dict, description="输出数据格式")
    code: str = Field(default="", description="Python算法代码")
    model_name: str = Field(default="", description="API训练后的模型名字")
    model_file: str = Field(default="", description="模型文件路径")
    api_doc: str = Field(default="", description="模型的API用法文档")


class AlgorithmUpdate(BaseModel):
    """更新算法模式"""
    name: Optional[str] = None
    description: Optional[str] = None
    type: Optional[str] = None
    status: Optional[str] = None


class AlgorithmVersionBase(BaseModel):
    """算法版本基础模式"""
    id: Optional[str] = None
    algorithm_id: str = Field(..., description="算法ID")
    version: str = Field(..., description="版本号")
    url: str = Field(..., description="算法API地址")
    params: Dict[str, Any] = Field(default_factory=dict, description="算法参数配置")
    input_schema: Dict[str, Any] = Field(default_factory=dict, description="输入数据格式")
    output_schema: Dict[str, Any] = Field(default_factory=dict, description="输出数据格式")
    code: str = Field(default="", description="Python算法代码")
    model_name: str = Field(default="", description="API训练后的模型名字")
    model_file: str = Field(default="", description="模型文件路径")
    api_doc: str = Field(default="", description="模型的API用法文档")
    is_default: bool = Field(default=False, description="是否为默认版本")


class AlgorithmVersionCreate(AlgorithmVersionBase):
    """创建算法版本模式"""
    pass


class AlgorithmVersionUpdate(BaseModel):
    """更新算法版本模式"""
    version: Optional[str] = None
    url: Optional[str] = None
    params: Optional[Dict[str, Any]] = None
    input_schema: Optional[Dict[str, Any]] = None
    output_schema: Optional[Dict[str, Any]] = None
    code: Optional[str] = None
    model_name: Optional[str] = None
    model_file: Optional[str] = None
    api_doc: Optional[str] = None
    is_default: Optional[bool] = None


class AlgorithmCallBase(BaseModel):
    """算法调用基础模式"""
    id: Optional[str] = None
    algorithm_id: str = Field(..., description="算法ID")
    version_id: str = Field(..., description="版本ID")
    input_data: Dict[str, Any] = Field(..., description="输入数据")
    params: Dict[str, Any] = Field(default_factory=dict, description="调用参数")


class AlgorithmCallCreate(AlgorithmCallBase):
    """创建算法调用模式"""
    pass


class AlgorithmCallResult(BaseModel):
    """算法调用结果模式"""
    id: str
    status: str
    output_data: Dict[str, Any]
    response_time: Optional[float] = None
    error_message: Optional[str] = None
    created_at: datetime


class AlgorithmResponse(AlgorithmBase):
    """算法响应模式"""
    status: str
    created_at: datetime
    updated_at: Optional[datetime] = None
    versions: List["AlgorithmVersionResponse"] = []


class AlgorithmVersionResponse(AlgorithmVersionBase):
    """算法版本响应模式"""
    created_at: datetime
    updated_at: Optional[datetime] = None


class AlgorithmListResponse(BaseModel):
    """算法列表响应模式"""
    algorithms: List[AlgorithmResponse]
    total: int


# 更新前向引用
AlgorithmResponse.model_rebuild()
