#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
在线脚本管理API模型定义
包含请求和响应的数据模型
"""

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

# 请求模型
class ScriptListParams(BaseModel):
    """脚本列表查询参数"""
    page: int = Field(1, description="页码，默认为1")
    pageSize: int = Field(20, description="每页记录数，默认为20")
    name: Optional[str] = Field(None, description="脚本名称，支持模糊查询", min_length=1, max_length=100)
    status: Optional[int] = Field(None, description="脚本状态(1:启用, 0:禁用)")
    folderPath: Optional[str] = Field(None, description="脚本所在目录路径", max_length=500)
    tags: Optional[str] = Field(None, description="标签，支持模糊查询")
    isPublic: Optional[int] = Field(None, description="是否公开(1:是, 0:否)")
    createdBy: Optional[str] = Field(None, description="创建者")
    startTime: Optional[str] = Field(None, description="创建时间范围的开始时间")
    endTime: Optional[str] = Field(None, description="创建时间范围的结束时间")
    sortField: str = Field("updated_on", description="排序字段")
    sortOrder: str = Field("desc", description="排序方式(asc/desc)")

class CreateScriptRequest(BaseModel):
    """创建脚本请求"""
    folderPath: Optional[str] = Field("/", description="脚本所在目录路径，默认为根目录", max_length=500)
    fileName: str = Field(..., description="脚本文件名，必须以.py结尾", min_length=1, max_length=255)
    code: str = Field(..., description="脚本代码内容")
    name: Optional[str] = Field(None, description="脚本名称，默认使用文件名(不含扩展名)", min_length=1, max_length=100)
    description: Optional[str] = Field(None, description="脚本功能描述")
    status: Optional[int] = Field(1, description="状态(1:启用, 0:禁用)")
    isPublic: Optional[int] = Field(1, description="是否公开(1:是, 0:否)")
    tags: Optional[str] = Field(None, description="标签，用于分类查询")
    testParams: Optional[str] = Field(None, description="测试参数(JSON格式)")

class UpdateScriptRequest(BaseModel):
    """更新脚本请求"""
    folderPath: Optional[str] = Field(None, description="脚本所在目录路径", max_length=500)
    fileName: Optional[str] = Field(None, description="脚本文件名，必须以.py结尾", min_length=1, max_length=255)
    code: Optional[str] = Field(None, description="脚本代码内容")
    name: Optional[str] = Field(None, description="脚本名称", min_length=1, max_length=100)
    description: Optional[str] = Field(None, description="脚本功能描述")
    status: Optional[int] = Field(None, description="状态(1:启用, 0:禁用)")
    isPublic: Optional[int] = Field(None, description="是否公开(1:是, 0:否)")
    tags: Optional[str] = Field(None, description="标签，用于分类查询")
    testParams: Optional[str] = Field(None, description="测试参数(JSON格式)")
    changeLog: Optional[str] = Field(None, description="版本变更说明")

class RunScriptRequest(BaseModel):
    """运行脚本请求"""
    params: Optional[Dict[str, Any]] = Field(None, description="脚本执行参数，JSON对象")
    taskRecordId: Optional[str] = Field(None, description="关联的任务记录ID")
    blockRecordId: Optional[str] = Field(None, description="关联的任务块记录ID")

class StopScriptRequest(BaseModel):
    """停止脚本执行请求"""
    logId: str = Field(..., description="脚本执行日志ID")

class RestoreScriptVersionRequest(BaseModel):
    """恢复脚本版本请求"""
    changeLog: Optional[str] = Field(None, description="版本变更说明")

# 响应模型
class ScriptInfo(BaseModel):
    """脚本信息"""
    id: str = Field(..., description="脚本ID")
    name: str = Field(..., description="脚本名称", min_length=1, max_length=100)
    folderPath: str = Field(..., description="脚本所在目录路径", max_length=500)
    fileName: str = Field(..., description="脚本文件名", min_length=1, max_length=255)
    description: Optional[str] = Field(None, description="脚本功能描述")
    version: int = Field(..., description="当前版本号")
    status: int = Field(..., description="状态(1:启用, 0:禁用)")
    isPublic: bool = Field(..., description="是否公开")
    tags: Optional[str] = Field(None, description="标签")
    createdBy: Optional[str] = Field(None, description="创建者")
    createdOn: Optional[str] = Field(None, description="创建时间")
    updatedBy: Optional[str] = Field(None, description="最后更新者")
    updatedOn: Optional[str] = Field(None, description="最后更新时间")

class ScriptDetail(ScriptInfo):
    """脚本详情，包含代码内容"""
    code: str = Field(..., description="脚本代码内容")
    testParams: Optional[str] = Field(None, description="测试参数(JSON格式)")

class ScriptVersionInfo(BaseModel):
    """脚本版本信息"""
    id: str = Field(..., description="版本记录ID")
    scriptId: str = Field(..., description="关联的脚本ID")
    version: int = Field(..., description="版本号")
    changeLog: Optional[str] = Field(None, description="版本变更说明")
    createdBy: Optional[str] = Field(None, description="创建者")
    createdOn: Optional[str] = Field(None, description="创建时间")

class ScriptVersionDetail(ScriptVersionInfo):
    """脚本版本详情，包含代码内容"""
    code: str = Field(..., description="该版本代码")

class ScriptLogInfo(BaseModel):
    """脚本执行日志信息"""
    id: str = Field(..., description="日志ID")
    scriptId: str = Field(..., description="关联的脚本ID")
    version: int = Field(..., description="使用的脚本版本")
    taskRecordId: Optional[str] = Field(None, description="关联的任务记录ID")
    blockRecordId: Optional[str] = Field(None, description="关联的任务块记录ID")
    inputParams: Optional[str] = Field(None, description="输入参数(JSON格式)")
    outputResult: Optional[str] = Field(None, description="输出结果(JSON格式)")
    status: int = Field(..., description="执行状态(1:成功, 0:失败)")
    errorMessage: Optional[str] = Field(None, description="错误信息")
    executionTime: Optional[int] = Field(None, description="执行耗时(毫秒)")
    startedOn: Optional[str] = Field(None, description="开始时间")
    endedOn: Optional[str] = Field(None, description="结束时间")

class ScriptLogDetail(ScriptLogInfo):
    """脚本执行日志详情"""
    scriptName: Optional[str] = Field(None, description="脚本名称")
    code: Optional[str] = Field(None, description="执行的脚本代码")

class FolderNode(BaseModel):
    """脚本目录节点"""
    name: str = Field(..., description="目录名称", min_length=1, max_length=100)
    path: str = Field(..., description="目录路径", max_length=500)
    children: List["FolderNode"] = Field(default_factory=list, description="子目录")

class ScriptCreateResponse(BaseModel):
    """创建脚本响应"""
    id: str = Field(..., description="脚本ID")

class ScriptUpdateResponse(BaseModel):
    """更新脚本响应"""
    version: int = Field(..., description="脚本版本号")

class ScriptRunResponse(BaseModel):
    """运行脚本响应"""
    logId: str = Field(..., description="脚本执行日志ID")
    result: Dict[str, Any] = Field(..., description="脚本执行结果")
    executionTime: int = Field(..., description="执行耗时(毫秒)")

class ScriptListResponse(BaseModel):
    """脚本列表响应"""
    total: int = Field(..., description="总记录数")
    list: List[ScriptInfo] = Field(..., description="脚本列表")

class ScriptVersionListResponse(BaseModel):
    """脚本版本列表响应"""
    total: int = Field(..., description="总记录数")
    list: List[ScriptVersionInfo] = Field(..., description="脚本版本列表")

class ScriptLogListResponse(BaseModel):
    """脚本执行日志列表响应"""
    total: int = Field(..., description="总记录数")
    list: List[ScriptLogInfo] = Field(..., description="脚本执行日志列表")

class FolderTreeResponse(RootModel):
    """脚本目录树响应"""
    root: List[FolderNode] = Field(..., description="目录树")

# 解决循环引用
FolderNode.update_forward_refs() 