# -*- coding: utf-8 -*-
"""
AI 工具定义
将 PS 操作注册为 function calling 的 tool schema
前端收到 tool_calls 后执行 JSX，把结果回传
"""

# ==================== 工具 Schema（OpenAI function calling 格式）====================

PS_TOOLS = [
    {
        "type": "function",
        "function": {
            "name": "get_document_info",
            "description": "获取当前 Photoshop 文档信息（名称、尺寸、分辨率）",
            "parameters": {
                "type": "object",
                "properties": {},
                "required": []
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "get_layer_structure",
            "description": "获取当前文档的图层结构树（所有顶层组和子图层）",
            "parameters": {
                "type": "object",
                "properties": {},
                "required": []
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "create_layer",
            "description": "在 Photoshop 中创建新图层",
            "parameters": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "description": "新图层的名称"
                    }
                },
                "required": ["name"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "rename_layer",
            "description": "重命名当前选中的图层",
            "parameters": {
                "type": "object",
                "properties": {
                    "new_name": {
                        "type": "string",
                        "description": "新名称"
                    }
                },
                "required": ["new_name"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "delete_layer",
            "description": "删除当前选中的图层",
            "parameters": {
                "type": "object",
                "properties": {},
                "required": []
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "duplicate_layer",
            "description": "复制当前选中的图层",
            "parameters": {
                "type": "object",
                "properties": {},
                "required": []
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "align_layers",
            "description": "对齐当前选中的图层",
            "parameters": {
                "type": "object",
                "properties": {
                    "type": {
                        "type": "string",
                        "enum": ["left", "right", "centerH", "top", "bottom", "centerV"],
                        "description": "对齐方式：left=左对齐, right=右对齐, centerH=水平居中, top=顶对齐, bottom=底对齐, centerV=垂直居中"
                    }
                },
                "required": ["type"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "merge_layers",
            "description": "合并当前选中的图层",
            "parameters": {
                "type": "object",
                "properties": {},
                "required": []
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "create_layer_group",
            "description": "创建图层组（如果不存在）",
            "parameters": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string",
                        "description": "图层组名称"
                    }
                },
                "required": ["name"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "move_layer_to_group",
            "description": "将当前图层移入指定图层组",
            "parameters": {
                "type": "object",
                "properties": {
                    "group_name": {
                        "type": "string",
                        "description": "目标图层组名称"
                    }
                },
                "required": ["group_name"]
            }
        }
    },
    # ==================== 套图工具 ====================
    {
        "type": "function",
        "function": {
            "name": "identify_pieces",
            "description": "识别裁片部位。截取当前PS画布并分析每个裁片图层的形状和位置，用视觉AI识别哪个图层是前片、后片、袖子等。应在套图前先调用，以建立图层名到裁片部位的对应关系。",
            "parameters": {
                "type": "object",
                "properties": {},
                "required": []
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "generate_garment_preview",
            "description": "【阶段1-生成预览】将用户上传的成衣照片中的花样，填充到 PS 文档的裁片轮廓上，生成一张带轮廓的花样预览图。预览图会显示在聊天中，供用户确认效果。需要用户已上传成衣图片。",
            "parameters": {
                "type": "object",
                "properties": {},
                "required": []
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "extract_and_apply_all_pieces",
            "description": "【阶段2-提取套图】用户确认预览 OK 后调用。根据 identify_pieces 的分析结果，对每个裁片执行不同操作：solid=PS纯色填充；fill_pattern=AI提取花型铺满；theme_pattern=底层纯色填充+上层AI提取主题图案(白底+正片叠底)；mixed_pattern=底层AI提取花型+上层AI提取主题图案(白底+正片叠底)。",
            "parameters": {
                "type": "object",
                "properties": {
                    "pieces": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "name": {"type": "string", "description": "裁片图层名称"},
                                "type": {"type": "string", "enum": ["solid", "fill_pattern", "theme_pattern", "mixed_pattern"], "description": "图案类型：solid=纯色；fill_pattern=花型铺满；theme_pattern=主题图案+纯色底；mixed_pattern=主题图案+花型底纹"},
                                "color": {"type": "string", "description": "颜色 hex 值。solid 和 theme_pattern 必须提供（theme_pattern 的 color 是底色）"},
                                "description": {"type": "string", "description": "图案内容描述"}
                             },
                            "required": ["name", "type"]
                        },
                        "description": "裁片列表，type 和 color 来自 identify_pieces 的分析结果"
                    }
                },
                "required": ["pieces"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "verify_pattern_result",
            "description": "验证套图效果。自动截取当前 PS 画布与原始成衣对比，由视觉 AI 评分(1-10)并给出改进建议。应在套图完成后调用。",
            "parameters": {
                "type": "object",
                "properties": {},
                "required": []
            }
        }
    },
]

# 工具名称映射（中文显示用）
TOOL_DISPLAY_NAMES = {
    "get_document_info": "查看文档信息",
    "get_layer_structure": "查看图层结构",
    "create_layer": "新建图层",
    "rename_layer": "重命名图层",
    "delete_layer": "删除图层",
    "duplicate_layer": "复制图层",
    "align_layers": "对齐图层",
    "merge_layers": "合并图层",
    "create_layer_group": "创建图层组",
    "move_layer_to_group": "移入图层组",
    "identify_pieces": "识别裁片部位",
    "generate_garment_preview": "生成花样预览",
    "extract_and_apply_all_pieces": "提取并套图",
    "verify_pattern_result": "验证套图效果",
}
