<?php

namespace XZD\AI\V5\Tools;

use XZD\AI\V5\Theme\ThemeManager;

/**
 * 工具注册器 — 一次性注册所有已实现的工具
 */
class ToolRegistrar
{
    public static function registerAll(): void
    {
        $r = ToolRegistry::instance();

        // Content tools
        $r->register('generate_tags', [
            'name' => '生成标签', 'description' => '为指定文章使用AI生成标签。支持AI模式/词库匹配/混合模式。',
            'category' => 'content', 'danger' => 'low', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'confirm_mode' => 'auto', 'undo_window' => 300, 'pipeline_stage' => true, 'pipeline_event' => 'publish_post',
            'track_processing' => true, 'supports_overwrite' => true,
            'schema' => [
                'type' => 'object', 'properties' => [
                    'post_id' => ['type' => 'integer', 'description' => '文章ID'],
                    'mode' => ['type' => 'string', 'enum' => ['ai', 'library', 'hybrid'], 'default' => 'ai'],
                    'count' => ['type' => 'integer', 'default' => 5], 'min_confidence' => ['type' => 'integer', 'default' => 60],
                ], 'required' => ['post_id'],
            ],
            'callback' => [ContentManager::class, 'generateTags'],
        ]);

        $r->register('generate_seo', [
            'name' => '生成SEO', 'description' => '为指定文章生成SEO标题、关键词和描述。',
            'category' => 'content', 'danger' => 'low', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'pipeline_stage' => true, 'pipeline_event' => 'publish_post',
            'track_processing' => true, 'supports_overwrite' => true,
            'schema' => ['type' => 'object', 'properties' => [
                'post_id'  => ['type' => 'integer', 'label' => '文章ID'],
                'title_len' => ['type' => 'integer', 'label' => '标题长度', 'default' => 60, 'description' => 'SEO标题最大字符数'],
                'desc_len'  => ['type' => 'integer', 'label' => '描述长度', 'default' => 160, 'description' => 'SEO描述最大字符数'],
            ], 'required' => ['post_id']],
            'callback' => [ContentManager::class, 'generateSEO'],
        ]);

        $r->register('generate_summary', [
            'name' => '生成摘要', 'description' => '为指定文章生成AI摘要（100字以内）。',
            'category' => 'content', 'danger' => 'low', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'pipeline_stage' => true, 'pipeline_event' => 'publish_post',
            'track_processing' => true, 'supports_overwrite' => true,
            'schema' => ['type' => 'object', 'properties' => ['post_id' => ['type' => 'integer'], 'length' => ['type' => 'integer', 'default' => 100]], 'required' => ['post_id']],
            'callback' => [ContentManager::class, 'generateSummary'],
        ]);

        // polish_content 在 build_autolink 之后注册（流水线中内链先构建再润色）

        $r->register('translate_content', [
            'name' => '翻译内容', 'description' => '将文章内容翻译为英文/日文/韩文等。',
            'category' => 'content', 'danger' => 'low', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'track_processing' => true, 'supports_overwrite' => true,
            'schema' => ['type' => 'object', 'properties' => ['post_id' => ['type' => 'integer'], 'target_lang' => ['type' => 'string', 'enum' => ['en', 'ja', 'ko', 'zh']]], 'required' => ['post_id']],
            'callback' => [ContentManager::class, 'translateContent'],
        ]);

        $r->register('rewrite_content', [
            'name' => '改写内容', 'description' => '以不同风格改写文章（专业化/通俗化/创意化）。',
            'category' => 'content', 'danger' => 'low', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'track_processing' => true, 'supports_overwrite' => true,
            'schema' => ['type' => 'object', 'properties' => ['post_id' => ['type' => 'integer'], 'style' => ['type' => 'string', 'enum' => ['professional', 'casual', 'creative']]], 'required' => ['post_id']],
            'callback' => [ContentManager::class, 'rewriteContent'],
        ]);

        $r->register('expand_content', [
            'name' => '扩展内容', 'description' => 'AI扩展文章内容，增加细节和示例。',
            'category' => 'content', 'danger' => 'low', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'track_processing' => true, 'supports_overwrite' => true,
            'schema' => ['type' => 'object', 'properties' => ['post_id' => ['type' => 'integer'], 'ratio' => ['type' => 'number', 'default' => 1.5]], 'required' => ['post_id']],
            'callback' => [ContentManager::class, 'expandContent'],
        ]);

        $r->register('generate_cover', [
            'name' => '生成封面图', 'description' => '为文章生成封面图。mode=prompt仅生成描述词，mode=full完整流程（描述词->图片->下载->设为特色图），四级降级链保底。',
            'category' => 'content', 'danger' => 'low', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'pipeline_stage' => true, 'pipeline_event' => 'publish_post', 'pipeline_async' => true,
            'track_processing' => true, 'supports_overwrite' => true,
            'schema' => ['type' => 'object', 'properties' => [
                'post_id' => ['type' => 'integer'],
                'style'   => ['type' => 'string', 'description' => '图片风格'],
                'mode'    => ['type' => 'string', 'enum' => ['prompt', 'full'], 'default' => 'prompt', 'description' => 'prompt=仅描述词 full=完整管线'],
            ], 'required' => ['post_id']],
            'callback' => [ContentManager::class, 'generateCover'],
        ]);

        $r->register('generate_alt', [
            'name' => '生成ALT', 'description' => '为文章内图片使用Vision AI生成alt文字并回写到文章HTML。',
            'category' => 'content', 'danger' => 'low', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'pipeline_stage' => true, 'pipeline_event' => 'publish_post', 'pipeline_async' => true,
            'track_processing' => true, 'supports_overwrite' => true,
            'schema' => ['type' => 'object', 'properties' => [
                'post_id' => ['type' => 'integer', 'label' => '文章ID'],
                'mode' => ['type' => 'string', 'label' => '生成模式', 'enum' => ['hybrid', 'ai', 'rule'], 'default' => 'hybrid', 'description' => 'AI+规则混合 / 纯AI视觉 / 规则模板'],
            ], 'required' => ['post_id']],
            'callback' => [ContentManager::class, 'generateALT'],
        ]);

        $r->register('build_autolink', [
            'name' => '构建内链', 'description' => '为文章自动推荐相关内链。',
            'category' => 'content', 'danger' => 'low', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'pipeline_stage' => true, 'pipeline_event' => 'publish_post',
            'track_processing' => true, 'supports_overwrite' => true,
            'schema' => ['type' => 'object', 'properties' => [
                'post_id' => ['type' => 'integer', 'label' => '文章ID'],
                'mode'  => ['type' => 'string', 'label' => '匹配模式', 'enum' => ['keyword', 'category', 'hybrid'], 'default' => 'keyword', 'description' => '关键词匹配 / 分类相关 / 混合推荐'],
                'count' => ['type' => 'integer', 'label' => '内链数量', 'default' => 5, 'description' => '每篇文章推荐的内链数量'],
            ], 'required' => ['post_id']],
            'callback' => [ContentManager::class, 'buildAutoLink'],
        ]);

        $r->register('polish_content', [
            'name' => '润色内容', 'description' => 'AI润色文章内容，优化表达和可读性。内链构建后执行，润色时会将内链融入文章。',
            'category' => 'content', 'danger' => 'low', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'pipeline_stage' => true, 'pipeline_event' => 'publish_post',
            'track_processing' => true, 'supports_overwrite' => true,
            'schema' => ['type' => 'object', 'properties' => ['post_id' => ['type' => 'integer'], 'depth' => ['type' => 'string', 'enum' => ['light', 'medium', 'deep']]], 'required' => ['post_id']],
            'callback' => [ContentManager::class, 'polishContent'],
        ]);

        $r->register('score_quality', [
            'name' => '质量评分', 'description' => '对文章进行AI质量评分（可读性/原创性/结构）。',
            'category' => 'content', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'user',
            'schema' => ['type' => 'object', 'properties' => ['post_id' => ['type' => 'integer']], 'required' => ['post_id']],
            'callback' => [ContentManager::class, 'scoreQuality'],
        ]);

        $r->register('check_duplicate', [
            'name' => '去重检测', 'description' => '检测文章是否存在重复内容。',
            'category' => 'content', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'user',
            'schema' => ['type' => 'object', 'properties' => ['post_id' => ['type' => 'integer']], 'required' => ['post_id']],
            'callback' => [ContentManager::class, 'checkDuplicate'],
        ]);

        // 单段落文本处理（编辑器块工具栏Popover）
        $r->register('process_text', [
            'name' => '文本处理', 'description' => '对单段落文本执行AI操作：润色/改写/扩写/缩短/翻译/语法修正。不写数据库，直接返回处理结果。',
            'category' => 'content', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'user',
            'schema' => ['type' => 'object', 'properties' => [
                'content' => ['type' => 'string', 'description' => '要处理的文本内容'],
                'action'  => ['type' => 'string', 'enum' => ['polish', 'rewrite', 'expand', 'shorten', 'translate_en', 'translate_ja', 'fixGrammar'], 'default' => 'polish'],
            ], 'required' => ['content', 'action']],
            'callback' => [ContentManager::class, 'processText'],
        ]);

        // 快速插入块（侧边栏一键生成）
        $r->register('generate_block', [
            'name' => '生成块', 'description' => '根据描述生成Gutenberg块并插入编辑器。block_type可选: code(代码块)/feature(亮点卡片)/alert(提醒框)/quote(引用块)。',
            'category' => 'content', 'danger' => 'none', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'schema' => ['type' => 'object', 'properties' => [
                'block_type' => ['type' => 'string', 'enum' => ['code', 'feature', 'alert', 'quote'], 'description' => '块类型'],
                'prompt'     => ['type' => 'string', 'description' => '内容描述'],
            ], 'required' => ['block_type', 'prompt']],
            'callback' => [ContentManager::class, 'generateBlock'],
        ]);

        // content_shorten / content_fixGrammar 已移除，被 process_text(action:shorten|fixGrammar) 替代

        // 文章标题扩写（编辑器面板用）
        $r->register('expand_title', [
            'name' => '标题扩写', 'description' => '根据文章标题和内容生成多个备选标题。title和content可选（留空则自动从文章读取）。',
            'category' => 'content', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'user',
            'schema' => ['type' => 'object', 'properties' => [
                'post_id' => ['type' => 'integer', 'description' => '文章ID'],
                'title'   => ['type' => 'string', 'description' => '当前标题（可选，留空自动读取）'],
                'content' => ['type' => 'string', 'description' => '文章正文片段（可选，留空自动读取）'],
                'count'   => ['type' => 'integer', 'default' => 3, 'description' => '生成标题数量'],
            ], 'required' => ['post_id']],
            'callback' => [ContentManager::class, 'expandTitle'],
        ]);

        // AI 编辑建议（编辑器面板用）
        $r->register('editorial_notes', [
            'name' => '编辑建议', 'description' => '对文章进行逐段分析，从可读性/SEO/语法三个维度给出修改建议。content参数可选（留空自动读取）。',
            'category' => 'content', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'user',
            'schema' => ['type' => 'object', 'properties' => [
                'post_id' => ['type' => 'integer', 'description' => '文章ID'],
                'content' => ['type' => 'string', 'description' => '文章正文（可选，留空自动读取全文）'],
                'sections' => ['type' => 'integer', 'default' => 3, 'description' => '分析维度数量'],
            ], 'required' => ['post_id']],
            'callback' => [ContentManager::class, 'editorialNotes'],
        ]);

        // AI 写文章
        $r->register('write_article', [
            'name' => 'AI写文章', 'description' => 'AI根据主题自动写文章并插入到指定分类。支持设置发布状态（草稿/发布）、文章内插图（0-5张，AI生成或免费图库）、自动生成封面图、自动生成SEO。例如："写一篇关于AI发展趋势的文章，发布到AI技术分类，配2张插图"。',
            'category' => 'content', 'danger' => 'medium', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'confirm_mode' => 'single', 'undo_window' => 600,
            'track_processing' => true, 'supports_overwrite' => true,
            'schema' => ['type' => 'object', 'properties' => [
                'topic'              => ['type' => 'string', 'description' => '写作主题/标题，如"2026年AI发展趋势"'],
                'keywords'           => ['type' => 'string', 'description' => '辅助关键词，逗号分隔，如"深度学习,大模型,AGI"'],
                'category_id'        => ['type' => 'integer', 'description' => '文章分类ID，不传则默认未分类'],
                'post_status'        => ['type' => 'string', 'default' => 'draft', 'description' => '发布状态：draft=草稿 publish=直接发布'],
                'tags'               => ['type' => 'string', 'description' => '预设标签，逗号分隔'],
                'illustration_count' => ['type' => 'integer', 'default' => 2, 'minimum' => 0, 'maximum' => 5, 'description' => '文章内插图数量，0=纯文字，建议2-3张'],
                'image_source'       => ['type' => 'string', 'default' => 'stock', 'enum' => ['ai', 'stock', 'public'], 'description' => '图片来源：ai=AI生成 stock=免费图库 public=公共域'],
                'generate_cover'     => ['type' => 'boolean', 'default' => true, 'description' => '是否自动生成文章封面图'],
                'generate_seo'       => ['type' => 'boolean', 'default' => true, 'description' => '是否自动生成SEO标题和描述'],
                'daily_max'          => ['type' => 'integer', 'default' => 3, 'minimum' => 0, 'description' => '每天最多写几篇，防刷量'],
            ], 'required' => ['topic']],
            'callback' => [ContentManager::class, 'writeArticle'],
        ]);

        // 恢复备份
        $r->register('restore_backup', [
            'name' => '恢复备份', 'description' => '将文章内容恢复为AI修改前的原文备份。',
            'category' => 'content', 'danger' => 'high', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'confirm_mode' => 'single', 'undo_window' => 0,
            'schema' => ['type' => 'object', 'properties' => [
                'post_id' => ['type' => 'integer', 'description' => '文章ID'],
            ], 'required' => ['post_id']],
            'callback' => [ContentManager::class, 'restoreBackup'],
        ]);

        // WordPress tools
        $wpTools = [
            ['wp_create_post', '创建文章', '创建新文章。', 'medium', 'edit_posts', 'single', 600, 'admin'],
            ['wp_update_post', '更新文章', '更新现有文章。', 'medium', 'edit_posts', 'single', 300, 'admin'],
            ['wp_delete_post', '删除文章', '永久删除文章。不可逆。', 'high', 'delete_posts', 'double', 0, 'admin'],
            ['wp_publish_post', '发布文章', '将文章发布上线。', 'medium', 'publish_posts', 'single', 0, 'admin'],
            ['wp_get_post', '查看文章', '查看文章详情。', 'none', 'read', 'auto', 0, 'guest'],
            ['wp_search_posts', '搜索文章', '搜索文章列表。', 'none', 'read', 'auto', 0, 'guest'],
            ['wp_list_users', '列出用户', '列出所有用户。', 'none', 'list_users', 'auto', 0, 'admin'],
            ['wp_get_user', '查询用户', '查询单个用户详情。', 'none', 'read', 'auto', 0, 'admin'],
            ['wp_create_user', '创建用户', '创建新用户。', 'high', 'create_users', 'single', 0, 'admin'],
            ['wp_update_user', '更新用户', '更新用户信息。', 'medium', 'edit_users', 'single', 0, 'admin'],
            ['wp_delete_user', '删除用户', '删除用户。', 'high', 'delete_users', 'double', 0, 'admin'],
            ['wp_change_role', '修改角色', '修改用户角色。', 'high', 'promote_users', 'double', 0, 'admin'],
            // V3.2: 插件在线更新
            ['wp_check_plugin_update', '检查插件更新', '检查小栈AI插件是否有新版本可用。返回当前版本、最新版本和更新日志。', 'none', 'update_plugins', 'auto', 0, 'admin'],
            ['wp_update_plugin', '更新插件', '下载并安装小栈AI插件最新版本。会自动备份旧版本。', 'high', 'update_plugins', 'double', 0, 'admin'],
            ['wp_site_stats', '站点统计', '获取站点概览统计。', 'none', 'read', 'auto', 0, 'guest'],
            ['wp_manage_comments', '管理评论', '管理评论（批准/垃圾/回收站）。', 'medium', 'moderate_comments', 'single', 0, 'admin'],
            ['wp_list_terms', '列出分类/标签', '只读列出网站的分类(category)或标签(post_tag)，不修改任何数据。', 'none', 'read', 'auto', 0, 'guest'],
            ['wp_manage_categories', '管理分类', '创建/删除分类（只查看请用 wp_list_terms）。', 'medium', 'manage_categories', 'single', 0, 'admin'],
            ['wp_manage_tags', '管理标签', '创建/删除标签（只查看请用 wp_list_terms）。', 'medium', 'manage_categories', 'single', 0, 'admin'],
            ['wp_list_plugins', '查看插件', '列出所有已安装插件。', 'none', 'manage_options', 'auto', 0, 'admin'],
            ['wp_get_settings', '查看设置', '查看WordPress基本设置。', 'none', 'manage_options', 'auto', 0, 'admin'],
            ['wp_clear_cache', '清除缓存', '清除页面缓存，加deep参数同时清理插件日志和过期数据。', 'low', 'manage_options', 'auto', 0, 'admin'],
            ['wp_reply_comment', '回复评论', '回复指定评论，留空内容则AI自动生成智能回复。', 'low', 'moderate_comments', 'auto', 0, 'admin'],
            ['cleanup_plugin_data', '清理插件数据', '清理操作日志、费用日志、自动化日志、已删会话、过期流分块、任务队列和遗留日志。', 'medium', 'manage_options', 'auto', 0, 'admin'],
        ];
        $wpCallbacks = [
            'createPost', 'updatePost', 'deletePost', 'publishPost', 'getPost', 'searchPosts',
            'listUsers', 'getUser', 'createUser', 'updateUser', 'deleteUser', 'changeRole',
            'siteStats', 'manageComments', 'listTerms', 'manageCategories', 'manageTags',
            'listPlugins', 'getSettings', 'clearCache', 'replyComment', 'cleanupPluginData',
            'checkPluginUpdate', 'updatePlugin',
        ];
        $wpSchemas = [
            'wp_create_post' => ['type' => 'object', 'properties' => ['title' => ['type' => 'string', 'description' => '文章标题'], 'content' => ['type' => 'string', 'description' => '文章内容'], 'post_status' => ['type' => 'string', 'enum' => ['draft', 'publish'], 'default' => 'draft'], 'category_id' => ['type' => 'integer', 'description' => '分类ID']], 'required' => ['title', 'content']],
            'wp_update_post' => ['type' => 'object', 'properties' => ['post_id' => ['type' => 'integer'], 'title' => ['type' => 'string'], 'content' => ['type' => 'string']], 'required' => ['post_id']],
            'wp_delete_post' => ['type' => 'object', 'properties' => ['post_id' => ['type' => 'integer'], 'force' => ['type' => 'boolean', 'default' => false]], 'required' => ['post_id']],
            'wp_publish_post' => ['type' => 'object', 'properties' => ['post_id' => ['type' => 'integer']], 'required' => ['post_id']],
            'wp_get_post' => ['type' => 'object', 'properties' => ['post_id' => ['type' => 'integer', 'description' => '文章ID']], 'required' => ['post_id']],
            'wp_search_posts' => ['type' => 'object', 'properties' => ['keyword' => ['type' => 'string', 'description' => '搜索关键词'], 'per_page' => ['type' => 'integer', 'description' => '返回文章数量', 'default' => 10], 'post_type' => ['type' => 'string', 'default' => 'post']], 'required' => []],
            'wp_list_users' => ['type' => 'object', 'properties' => ['per_page' => ['type' => 'integer', 'description' => '每页数量', 'default' => 20]], 'required' => []],
            'wp_get_user' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer', 'description' => '用户ID']], 'required' => ['user_id']],
            'wp_create_user' => ['type' => 'object', 'properties' => ['name' => ['type' => 'string', 'description' => '用户名'], 'email' => ['type' => 'string'], 'password' => ['type' => 'string'], 'role' => ['type' => 'string', 'default' => 'subscriber']], 'required' => ['name', 'email']],
            'wp_update_user' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer'], 'name' => ['type' => 'string'], 'email' => ['type' => 'string'], 'role' => ['type' => 'string']], 'required' => ['user_id']],
            'wp_delete_user' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer']], 'required' => ['user_id']],
            'wp_change_role' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer'], 'role' => ['type' => 'string']], 'required' => ['user_id', 'role']],
            'wp_site_stats' => ['type' => 'object', 'properties' => new \stdClass(), 'required' => []],
            'wp_manage_comments' => ['type' => 'object', 'properties' => ['action' => ['type' => 'string', 'enum' => ['approve', 'spam', 'trash']], 'comment_id' => ['type' => 'integer']], 'required' => ['action', 'comment_id']],
            'wp_list_terms' => ['type' => 'object', 'properties' => ['taxonomy' => ['type' => 'string', 'enum' => ['category', 'post_tag'], 'description' => '分类法：category=分类，post_tag=标签'], 'hide_empty' => ['type' => 'boolean', 'description' => '是否隐藏空分类'], 'per_page' => ['type' => 'integer', 'default' => 50]], 'required' => []],
            'wp_manage_categories' => ['type' => 'object', 'properties' => ['action' => ['type' => 'string', 'enum' => ['create', 'delete', 'list']], 'name' => ['type' => 'string'], 'parent' => ['type' => 'integer'], 'category_id' => ['type' => 'integer']], 'required' => ['action']],
            'wp_manage_tags' => ['type' => 'object', 'properties' => ['action' => ['type' => 'string', 'enum' => ['create', 'delete', 'list']], 'name' => ['type' => 'string'], 'tag_id' => ['type' => 'integer']], 'required' => ['action']],
            'wp_clear_cache' => ['type' => 'object', 'properties' => ['deep' => ['type' => 'boolean', 'description' => '深度清理插件日志和过期数据']], 'required' => []],
            'wp_reply_comment' => ['type' => 'object', 'properties' => ['comment_id' => ['type' => 'integer'], 'content' => ['type' => 'string', 'description' => '回复内容，留空则AI自动生成'], 'post_id' => ['type' => 'integer'], 'auto_approve' => ['type' => 'boolean', 'description' => 'AI回复是否自动过审，默认true']], 'required' => ['comment_id']],
            'wp_moderate_comment' => ['type' => 'object', 'properties' => [
                'comment_id'      => ['type' => 'integer'],
                'auto_moderate'   => ['type' => 'boolean', 'default' => true, 'description' => '启用AI自动审核'],
                'strictness'      => ['type' => 'string', 'enum' => ['low', 'medium', 'high'], 'default' => 'medium', 'description' => '审核严格度'],
                'min_words'       => ['type' => 'integer', 'default' => 2, 'description' => '最低字数'],
                'block_urls'      => ['type' => 'boolean', 'default' => true, 'description' => '含链接的评论直接待审'],
                'sensitive_words' => ['type' => 'string', 'default' => '', 'description' => '敏感词，逗号分隔'],
            ], 'required' => ['comment_id']],
            'cleanup_plugin_data' => ['type' => 'object', 'properties' => ['retention_days' => ['type' => 'integer', 'description' => '保留天数，默认7']], 'required' => []],
            'wp_check_plugin_update' => ['type' => 'object', 'properties' => new \stdClass(), 'required' => []],
            'wp_update_plugin' => ['type' => 'object', 'properties' => new \stdClass(), 'required' => []],
        ];
        foreach ($wpTools as $i => $t) {
            $r->register($t[0], [
                'name' => $t[1], 'description' => $t[2], 'category' => 'wp', 'danger' => $t[3],
                'wp_cap' => $t[4], 'min_role' => $t[7], 'confirm_mode' => $t[5], 'undo_window' => $t[6],
                'schema' => $wpSchemas[$t[0]] ?? ['type' => 'object', 'properties' => [], 'required' => []],
                'callback' => [WPManager::class, $wpCallbacks[$i]],
            ]);
        }

        // 评论审核工具（独立类）
        $r->register('wp_moderate_comment', [
            'name' => 'AI评论审核', 'description' => 'AI自动审核评论质量，判断是否通过。支持快速规则+AI深度判断。',
            'category' => 'wp', 'danger' => 'low',
            'wp_cap' => 'moderate_comments', 'min_role' => 'admin',
            'schema' => $wpSchemas['wp_moderate_comment'] ?? [],
            'callback' => [CommentModerator::class, 'moderate'],
        ]);

        // SEO tools — pipeline_stage tools: score_seo, generate_schema, push_search
        $pipelineSeoSchemas = [
            'score_seo' => ['type' => 'object', 'properties' => [
                'post_id'  => ['type' => 'integer', 'label' => '文章ID'],
                'auto_fix' => ['type' => 'string', 'label' => '自动优化', 'enum' => ['false', 'suggest'], 'default' => 'false', 'description' => '仅评分 / 评分+给出优化建议'],
            ], 'required' => ['post_id']],
            'generate_schema' => ['type' => 'object', 'properties' => [
                'post_id'    => ['type' => 'integer', 'label' => '文章ID'],
                'schema_type' => ['type' => 'string', 'label' => 'Schema类型', 'enum' => ['Article', 'FAQPage', 'Product'], 'default' => 'Article', 'description' => '结构化数据类型：文章/FAQ/产品'],
            ], 'required' => ['post_id']],
            'push_search' => ['type' => 'object', 'properties' => [
                'post_id'     => ['type' => 'integer', 'label' => '文章ID'],
                'baidu_token' => ['type' => 'string', 'label' => '百度推送Token', 'description' => '百度搜索资源平台API Token，留空跳过'],
                'bing_key'    => ['type' => 'string', 'label' => 'Bing IndexNow Key', 'description' => 'Bing Webmaster IndexNow API Key，留空跳过'],
                'google_key'  => ['type' => 'string', 'label' => 'Google推送Key', 'description' => 'Google Indexing API Key，留空跳过'],
            ], 'required' => ['post_id']],
        ];
        $pipelineSeoTools = [
            ['score_seo', 'SEO评分', '对文章SEO状况评分。', [SEOManager::class, 'scoreSEO']],
            ['generate_schema', '生成Schema', '生成Schema.org结构化数据。', [SEOManager::class, 'generateSchema']],
            ['push_search', '搜索推送', '推送文章到百度/Bing。', [SEOManager::class, 'pushSearch']],
        ];
        foreach ($pipelineSeoTools as $t) {
            $r->register($t[0], [
                'name' => $t[1], 'description' => $t[2], 'category' => 'seo', 'danger' => 'low',
                'wp_cap' => 'edit_posts', 'min_role' => 'user',
                'confirm_mode' => 'auto', 'pipeline_stage' => true, 'pipeline_event' => 'publish_post',
                'track_processing' => true,
                'schema' => $pipelineSeoSchemas[$t[0]],
                'callback' => $t[3],
            ]);
        }
        // Non-pipeline SEO tools
        $r->register('scan_deadlink', [
            'name' => '扫描死链', 'description' => '扫描文章中的死链接。',
            'category' => 'seo', 'danger' => 'low', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'schema' => ['type' => 'object', 'properties' => ['post_id' => ['type' => 'integer']], 'required' => ['post_id']],
            'callback' => [SEOManager::class, 'scanDeadLink'],
        ]);
        $r->register('run_pipeline', [
            'name' => '执行流水线', 'description' => '手动执行文章处理流水线。',
            'category' => 'seo', 'danger' => 'low', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'schema' => ['type' => 'object', 'properties' => ['post_id' => ['type' => 'integer']], 'required' => ['post_id']],
            'callback' => [SEOManager::class, 'runPipeline'],
        ]);

        // Media tools
        $mediaSchemas = [
            'apply_watermark' => ['type' => 'object', 'properties' => [
                'post_id'       => ['type' => 'integer', 'label' => '文章ID'],
                'watermark_id'  => ['type' => 'integer', 'label' => '水印图片', 'description' => '选择水印图片，留空使用全局默认', 'xzd_control' => 'media_picker'],
                'position'      => ['type' => 'string', 'label' => '水印位置', 'enum' => ['top-left', 'top-center', 'top-right', 'center-left', 'center', 'center-right', 'bottom-left', 'bottom-center', 'bottom-right'], 'default' => 'bottom-right', 'description' => '九宫格位置：左上/上中/右上/左中/居中/右中/左下/下中/右下'],
                'opacity'       => ['type' => 'integer', 'label' => '透明度', 'minimum' => 10, 'maximum' => 100, 'default' => 50, 'description' => '水印透明度，10=几乎透明，100=完全不透明'],
                'size_pct'      => ['type' => 'integer', 'label' => '水印大小', 'minimum' => 5, 'maximum' => 50, 'default' => 15, 'description' => '水印占图片宽度的百分比'],
            ], 'required' => ['post_id']],
            'localize_image' => ['type' => 'object', 'properties' => [
                'post_id'    => ['type' => 'integer', 'label' => '文章ID'],
                'max_images' => ['type' => 'integer', 'label' => '最大处理数', 'default' => 20, 'description' => '最多下载多少张外部图片到本地'],
            ], 'required' => ['post_id']],
            'compress_image' => ['type' => 'object', 'properties' => [
                'post_id' => ['type' => 'integer', 'label' => '文章ID'],
                'quality' => ['type' => 'integer', 'label' => '压缩质量', 'default' => 85, 'description' => '图片压缩质量，100=无损，50=高压缩'],
            ], 'required' => ['post_id']],
            'convert_to_webp' => ['type' => 'object', 'properties' => [
                'post_id'    => ['type' => 'integer', 'label' => '文章ID'],
                'quality'    => ['type' => 'integer', 'label' => 'WebP质量', 'default' => 85, 'description' => 'WebP转换质量，100=无损，50=高压缩'],
                'max_images' => ['type' => 'integer', 'label' => '最大处理数', 'default' => 20, 'description' => '最多转换多少张图片为WebP'],
            ], 'required' => ['post_id']],
            'process_images' => ['type' => 'object', 'properties' => [
                'post_id'       => ['type' => 'integer', 'label' => '文章ID'],
                'skip_localize' => ['type' => 'boolean', 'label' => '跳过本地化', 'default' => false, 'description' => '图片已在本地时跳过下载步骤'],
                'watermark_id'  => ['type' => 'integer', 'label' => '水印图片', 'description' => '留空使用全局默认'],
                'webp_quality'  => ['type' => 'integer', 'label' => 'WebP质量', 'default' => 85],
                'compress_quality' => ['type' => 'integer', 'label' => '压缩质量', 'default' => 85],
            ], 'required' => ['post_id']],
        ];
        $mediaTools = [
            ['apply_watermark', '应用水印', '为文章图片应用水印。', [MediaManager::class, 'applyWatermark']],
            ['localize_image', '图片本地化', '将外部图片下载到本地。', [MediaManager::class, 'localizeImage']],
            ['compress_image', '压缩图片', '压缩文章中的图片。', [MediaManager::class, 'compressImage']],
            ['convert_to_webp', 'WebP转换', '将文章内PNG/JPEG图片转为WebP格式，保留原图作为降级。使用picture标签保证浏览器兼容。', [MediaManager::class, 'convertToWebp']],
            ['process_images', '一键处理图片', '依次执行：本地化->WebP转换->水印->压缩，一站式处理文章所有图片。', [MediaManager::class, 'processImages']],
        ];
        foreach ($mediaTools as $t) {
            $r->register($t[0], [
                'name' => $t[1], 'description' => $t[2], 'category' => 'media', 'danger' => 'low',
                'wp_cap' => 'edit_posts', 'min_role' => 'user',
                'confirm_mode' => 'auto', 'pipeline_stage' => true, 'pipeline_event' => 'publish_post', 'pipeline_async' => true,
                'track_processing' => true,
                'schema' => $mediaSchemas[$t[0]],
                'callback' => $t[3],
            ]);
        }

        // 图片搜索 & 生成
        $r->register('search_stock', [
            'name' => '搜索图库', 'description' => '搜索免费图库（Pexels/Unsplash/Pixabay）和公共域图库（Lorem Picsum），无需配置即可使用公共域保底。',
            'category' => 'media', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'user',
            'schema' => ['type' => 'object', 'properties' => [
                'query'   => ['type' => 'string', 'description' => '搜索关键词'],
                'per_page' => ['type' => 'integer', 'default' => 10],
            ], 'required' => ['query']],
            'callback' => [MediaManager::class, 'searchStock'],
        ]);

        $r->register('generate_inline_image', [
            'name' => '生成插图', 'description' => '为文章段落生成插图。根据上下文自动选择AI生成/图库/公共域三级降级，下载到媒体库。',
            'category' => 'media', 'danger' => 'low', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'schema' => ['type' => 'object', 'properties' => [
                'post_id'      => ['type' => 'integer', 'description' => '文章ID'],
                'context_text' => ['type' => 'string', 'description' => '上下文段落文字'],
            ], 'required' => ['post_id']],
            'callback' => [MediaManager::class, 'generateInlineImage'],
        ]);

        // Friend Link tools
        $r->register('list_friend_links', [
            'name' => '列出友链', 'description' => '列出所有友链，含互链状态(正常/丢失/不确定/白名单)、分类、可见性。传check_all=true触发批量互链检测（替代旧check_all_friend_links）。传link_id可验证单条。',
            'category' => 'seo', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'guest',
            'schema' => ['type' => 'object', 'properties' => [
                'check_all'   => ['type' => 'boolean', 'description' => '是否批量检测全部已通过友链'],
                'batch_limit' => ['type' => 'integer', 'default' => 20, 'description' => 'check_all时单次检测上限'],
                'link_id'     => ['type' => 'integer', 'description' => '验证单条友链时传入'],
            ], 'required' => []],
            'callback' => [FriendLinkManager::class, 'listFriendLinks'],
        ]);
        $r->register('add_friend_link', [
            'name' => '添加友链', 'description' => '通过URL添加友链，自动抓取网站标题/图标/描述，检测互链状态。互链自动通过，未互链标记为待审。',
            'category' => 'seo', 'danger' => 'medium', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'confirm_mode' => 'single',
            'schema' => ['type' => 'object', 'properties' => [
                'url'         => ['type' => 'string', 'description' => '对方网站URL（必填）'],
                'name'        => ['type' => 'string', 'description' => '友链名称（可选，留空自动抓取）'],
                'category'    => ['type' => 'string', 'description' => '友链分类名（可选，留空自动识别）'],
                'description' => ['type' => 'string', 'description' => '网站描述（可选）'],
                'image'       => ['type' => 'string', 'description' => 'Logo图片URL（可选）'],
            ], 'required' => ['url']],
            'callback' => [FriendLinkManager::class, 'addFriendLink'],
        ]);
        $r->register('delete_friend_link', [
            'name' => '删除友链', 'description' => '永久删除友链（仅管理员手动操作）。支持按ID或URL指定。',
            'category' => 'seo', 'danger' => 'medium', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'confirm_mode' => 'single',
            'schema' => ['type' => 'object', 'properties' => [
                'link_id'  => ['type' => 'integer', 'description' => '友链ID（与URL二选一）'],
                'link_url' => ['type' => 'string', 'description' => '友链URL（与ID二选一）'],
            ], 'required' => []],
            'callback' => [FriendLinkManager::class, 'deleteFriendLink'],
        ]);
        $r->register('review_friend_link', [
            'name' => '审核友链', 'description' => '审核友链申请：action=approve通过（设为可见+通知），action=reject拒绝（记录原因+通知）。',
            'category' => 'seo', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [
                'link_id' => ['type' => 'integer', 'description' => '友链ID'],
                'action'  => ['type' => 'string', 'enum' => ['approve', 'reject'], 'description' => 'approve=通过 reject=拒绝'],
                'notes'   => ['type' => 'string', 'description' => '备注（approve时）'],
                'reason'  => ['type' => 'string', 'description' => '拒绝原因（reject时）'],
            ], 'required' => ['link_id', 'action']],
            'callback' => [FriendLinkManager::class, 'reviewFriendLink'],
        ]);
        $r->register('toggle_link_whitelist', [
            'name' => '友链白名单', 'description' => '将友链加入/移出白名单。白名单内的友链跳过自动互链检测（适用于有GO跳转、CDN验证等特殊站点）。',
            'category' => 'seo', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [
                'link_id' => ['type' => 'integer', 'description' => '友链ID'],
            ], 'required' => ['link_id']],
            'callback' => [FriendLinkManager::class, 'toggleLinkWhitelist'],
        ]);

        // Knowledge tools
        $r->register('search_knowledge', [
            'name' => '搜索知识库', 'description' => '在RAG知识库中搜索相关内容。',
            'category' => 'knowledge', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'guest',
            'schema' => ['type' => 'object', 'properties' => [
                'keyword'          => ['type' => 'string'],
                'limit'            => ['type' => 'integer', 'default' => 10],
                'learn_posts'      => ['type' => 'boolean', 'default' => true, 'description' => '学习文章内容'],
                'learn_pages'      => ['type' => 'boolean', 'default' => true, 'description' => '学习页面内容'],
                'learn_products'   => ['type' => 'boolean', 'default' => true, 'description' => '学习商品内容'],
                'learn_forum'      => ['type' => 'boolean', 'default' => true, 'description' => '学习论坛内容'],
                'learn_membership' => ['type' => 'boolean', 'default' => true, 'description' => '学习会员体系'],
                'schedule_hour'    => ['type' => 'integer', 'default' => 3, 'minimum' => 0, 'maximum' => 23, 'description' => '每日学习执行时间'],
                'batch_size'       => ['type' => 'integer', 'default' => 50, 'description' => '每批学习数量'],
                'expire_days'      => ['type' => 'integer', 'default' => 90, 'description' => '知识过期天数'],
            ], 'required' => ['keyword']],
            'callback' => [KnowledgeManager::class, 'search'],
        ]);
        $r->register('add_faq', [
            'name' => '添加FAQ', 'description' => '向FAQ列表添加问答对。',
            'category' => 'knowledge', 'danger' => 'low', 'wp_cap' => 'edit_posts', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => ['question' => ['type' => 'string'], 'answer' => ['type' => 'string'], 'category' => ['type' => 'string']], 'required' => ['question', 'answer']],
            'callback' => [KnowledgeManager::class, 'addFaq'],
        ]);
        $r->register('delete_faq', [
            'name' => '删除FAQ', 'description' => '从FAQ列表中删除指定项。',
            'category' => 'knowledge', 'danger' => 'medium', 'wp_cap' => 'edit_posts', 'min_role' => 'admin', 'confirm_mode' => 'single',
            'schema' => ['type' => 'object', 'properties' => ['faq_id' => ['type' => 'integer']], 'required' => ['faq_id']],
            'callback' => [KnowledgeManager::class, 'deleteFaq'],
        ]);
        $r->register('rebuild_index', [
            'name' => '重建索引', 'description' => '重建知识库全文索引。',
            'category' => 'knowledge', 'danger' => 'medium', 'wp_cap' => 'manage_options', 'min_role' => 'admin', 'confirm_mode' => 'single',
            'schema' => ['type' => 'object', 'properties' => []],
            'callback' => [KnowledgeManager::class, 'rebuildIndex'],
        ]);

        // Automation tools — create_write_plan 已移除，被 schedule_write 替代
        $r->register('trigger_write_now', [
            'name' => '立即写文章', 'description' => '立即执行写文章计划。',
            'category' => 'automation', 'danger' => 'medium', 'wp_cap' => 'edit_posts', 'min_role' => 'admin', 'confirm_mode' => 'single',
            'schema' => ['type' => 'object', 'properties' => ['plan_id' => ['type' => 'integer'], 'topic' => ['type' => 'string']]],
            'callback' => [AutomationManager::class, 'triggerWriteNow'],
        ]);
        // view_queue 已移除，被 check_processing_queue 替代

        // 自动化规则管理工具
        $r->register('get_available_events', [
            'name' => '获取触发事件', 'description' => '获取可用的自动化触发事件列表。',
            'category' => 'automation', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => []],
            'callback' => [AutomationManager::class, 'getAvailableEvents'],
        ]);

        $r->register('get_available_actions', [
            'name' => '获取可用动作', 'description' => '获取可用的自动化执行动作列表（所有已注册工具）。',
            'category' => 'automation', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => []],
            'callback' => [AutomationManager::class, 'getAvailableActions'],
        ]);

        $r->register('create_automation_rule', [
            'name' => '创建自动化规则', 'description' => '创建新的自动化规则。AI可在对话中调用此工具为用户配置自动化。',
            'category' => 'automation', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => [
                'type' => 'object',
                'properties' => [
                    'name' => ['type' => 'string', 'description' => '规则名称'],
                    'event' => ['type' => 'string', 'description' => '触发事件ID，从get_available_events获取'],
                    'actions' => ['type' => 'array', 'description' => '执行动作列表 [{"tool_id":"...","params":{}}]'],
                    'description' => ['type' => 'string', 'description' => '规则描述'],
                    'conditions' => ['type' => 'object', 'description' => '触发条件（可选）'],
                    'enabled' => ['type' => 'boolean', 'description' => '是否立即启用'],
                    'rule_type' => ['type' => 'string', 'description' => '规则类型: permanent(永久), oneshot(一次), expiring(过期), limited(限次)'],
                    'max_executions' => ['type' => 'integer', 'description' => '最大执行次数（rule_type=limited时有效）'],
                    'expires_at' => ['type' => 'string', 'description' => '过期时间（rule_type=expiring时有效）'],
                ],
                'required' => ['name', 'event', 'actions'],
            ],
            'callback' => [AutomationManager::class, 'createRule'],
        ]);

        $r->register('list_automation_rules', [
            'name' => '列出规则', 'description' => '列出所有自动化规则。',
            'category' => 'automation', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => []],
            'callback' => [AutomationManager::class, 'listRules'],
        ]);

        $r->register('get_automation_rule', [
            'name' => '查看规则', 'description' => '查看单条自动化规则详情。',
            'category' => 'automation', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => ['rule_id' => ['type' => 'integer']], 'required' => ['rule_id']],
            'callback' => [AutomationManager::class, 'getRule'],
        ]);

        $r->register('update_automation_rule', [
            'name' => '修改规则', 'description' => '修改已有的自动化规则。',
            'category' => 'automation', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => [
                'type' => 'object',
                'properties' => [
                    'rule_id' => ['type' => 'integer', 'description' => '规则ID'],
                    'name' => ['type' => 'string'], 'event' => ['type' => 'string'],
                    'actions' => ['type' => 'array'], 'enabled' => ['type' => 'boolean'],
                ],
                'required' => ['rule_id'],
            ],
            'callback' => [AutomationManager::class, 'updateRule'],
        ]);

        $r->register('delete_automation_rule', [
            'name' => '删除规则', 'description' => '删除指定的自动化规则。',
            'category' => 'automation', 'danger' => 'medium', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'confirm_mode' => 'single',
            'schema' => ['type' => 'object', 'properties' => ['rule_id' => ['type' => 'integer']], 'required' => ['rule_id']],
            'callback' => [AutomationManager::class, 'deleteRule'],
        ]);

        $r->register('toggle_automation_rule', [
            'name' => '启用/禁用规则', 'description' => '切换自动化规则的启用/禁用状态。',
            'category' => 'automation', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => ['rule_id' => ['type' => 'integer']], 'required' => ['rule_id']],
            'callback' => [AutomationManager::class, 'toggleRule'],
        ]);

        $r->register('run_automation_rule', [
            'name' => '手动执行规则', 'description' => '手动执行指定的自动化规则。',
            'category' => 'automation', 'danger' => 'medium', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'confirm_mode' => 'single',
            'schema' => ['type' => 'object', 'properties' => ['rule_id' => ['type' => 'integer']], 'required' => ['rule_id']],
            'callback' => [AutomationManager::class, 'runRule'],
        ]);

        // 对话工具增强 — 批量处理 & 定时写文章
        $r->register('batch_process', [
            'name' => '批量处理', 'description' => '按范围筛选文章并批量执行流水线处理。支持最近N天/全部/指定ID/日期范围/分类筛选，可指定处理阶段。',
            'category' => 'automation', 'danger' => 'medium', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'confirm_mode' => 'single',
            'schema' => ['type' => 'object', 'properties' => [
                'range'       => ['type' => 'string', 'enum' => ['recent', 'all', 'ids', 'date_range', 'category'], 'default' => 'recent', 'description' => '文章范围：recent=最近30天, all=全部, ids=指定ID, date_range=日期范围, category=按分类'],
                'date_from'   => ['type' => 'string', 'description' => '起始日期(YYYY-MM-DD)，range=date_range时使用'],
                'date_to'     => ['type' => 'string', 'description' => '结束日期(YYYY-MM-DD)，range=date_range时使用'],
                'category_id' => ['type' => 'integer', 'description' => '分类ID，range=category时必填; range=recent/date_range时可选'],
                'post_ids'    => ['type' => 'array', 'description' => '文章ID数组，range=ids时必填'],
                'stages'      => ['type' => 'array', 'description' => '处理阶段列表，如["generate_tags","generate_seo","generate_summary"]，默认标签+SEO+摘要'],
                'post_status' => ['type' => 'string', 'default' => 'publish', 'description' => '文章状态筛选'],
                'max_posts'   => ['type' => 'integer', 'default' => 50, 'description' => '最大处理文章数，上限200'],
                'batch_size'  => ['type' => 'integer', 'description' => '每批处理数量，默认自动检测'],
            ], 'required' => []],
            'callback' => [AutomationManager::class, 'batchProcess'],
        ]);

        $r->register('schedule_write', [
            'name' => '定时写文章', 'description' => '创建AI定时自动写文章规则。设置每天几点、写什么主题、发布到哪个分类、每篇文章配几张插图。例如："每天早上8点写一篇AI行业动态，发布到AI技术分类，配2张插图"。',
            'category' => 'automation', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'confirm_mode' => 'single',
            'schema' => ['type' => 'object', 'properties' => [
                'topic'              => ['type' => 'string', 'description' => '写作主题，如"AI行业日报"'],
                'schedule_hours'     => ['type' => 'array', 'description' => '每天几点写（0-23），如[8]表示早上8点，[8,18]表示早晚各一篇'],
                'keywords'           => ['type' => 'string', 'description' => '辅助关键词，帮助AI确定写作方向'],
                'category_id'        => ['type' => 'integer', 'description' => '发布到哪个分类（分类ID）'],
                'post_status'        => ['type' => 'string', 'enum' => ['draft', 'publish', 'pending'], 'default' => 'draft', 'description' => '发布状态：draft=草稿 publish=直接发布'],
                'tags'               => ['type' => 'string', 'description' => '文章标签，逗号分隔'],
                'illustration_count' => ['type' => 'integer', 'default' => 2, 'minimum' => 0, 'maximum' => 5, 'description' => '每篇文章插图数量'],
                'image_source'       => ['type' => 'string', 'enum' => ['ai', 'stock', 'public'], 'default' => 'stock', 'description' => '图片来源'],
                'generate_cover'     => ['type' => 'boolean', 'default' => true, 'description' => '自动生成封面图'],
                'generate_seo'       => ['type' => 'boolean', 'default' => true, 'description' => '自动生成SEO'],
                'daily_max'          => ['type' => 'integer', 'default' => 3, 'description' => '每天最多写几篇'],
                'enabled'            => ['type' => 'boolean', 'default' => true, 'description' => '创建后是否立即启用'],
                'name'               => ['type' => 'string', 'description' => '规则名称，留空自动生成'],
            ], 'required' => ['topic']],
            'callback' => [AutomationManager::class, 'scheduleWrite'],
        ]);

        $r->register('list_scheduled_writes', [
            'name' => '查看定时写文章', 'description' => '查看当前所有AI定时写文章规则及其状态。',
            'category' => 'automation', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [], 'required' => []],
            'callback' => [AutomationManager::class, 'listScheduledWrites'],
        ]);

        $r->register('delete_scheduled_write', [
            'name' => '删除定时写文章', 'description' => '删除指定的定时写文章规则。',
            'category' => 'automation', 'danger' => 'medium', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'confirm_mode' => 'single',
            'schema' => ['type' => 'object', 'properties' => [
                'rule_id' => ['type' => 'integer', 'description' => '定时写文章规则ID'],
            ], 'required' => ['rule_id']],
            'callback' => [AutomationManager::class, 'deleteScheduledWrite'],
        ]);

        $r->register('check_processing_queue', [
            'name' => '查看处理队列', 'description' => '查看当前多Worker处理队列状态：待处理数、各来源分布、活跃Worker数、健康状态。',
            'category' => 'automation', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [], 'required' => []],
            'callback' => [AutomationManager::class, 'checkQueue'],
        ]);

        $r->register('worker_status', [
            'name' => 'Worker状态', 'description' => '查看AI处理系统Worker运行状态：今日处理量、平均耗时、内存峰值、速率限制器状态。',
            'category' => 'automation', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [], 'required' => []],
            'callback' => [AutomationManager::class, 'workerStatus'],
        ]);

        // Theme tools
        // 积分系统
        $r->register('theme_get_points', [
            'name' => '查询积分', 'description' => '查询积分余额。不传user_id则查自己，管理员可查任何人。',
            'category' => 'theme', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'user',
            'theme_required' => true, 'capability_key' => 'points',
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer', 'description' => '用户ID，留空为当前用户']], 'required' => []],
            'callback' => [ThemeManager::class, 'getPoints'],
        ]);
        $r->register('theme_add_points', [
            'name' => '增加积分', 'description' => '为指定用户增加积分。',
            'category' => 'theme', 'danger' => 'medium', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'theme_required' => true, 'capability_key' => 'points', 'confirm_mode' => 'single', 'undo_window' => 300,
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer'], 'amount' => ['type' => 'integer'], 'reason' => ['type' => 'string']], 'required' => ['user_id', 'amount']],
            'callback' => [ThemeManager::class, 'addPoints'],
        ]);
        $r->register('theme_deduct_points', [
            'name' => '扣减积分', 'description' => '从指定用户扣减积分。',
            'category' => 'theme', 'danger' => 'medium', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'theme_required' => true, 'capability_key' => 'points', 'confirm_mode' => 'single', 'undo_window' => 300,
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer'], 'amount' => ['type' => 'integer'], 'reason' => ['type' => 'string']], 'required' => ['user_id', 'amount']],
            'callback' => [ThemeManager::class, 'deductPoints'],
        ]);

        // 余额系统
        $r->register('theme_get_balance', [
            'name' => '查询余额', 'description' => '查询余额。不传user_id则查自己，管理员可查任何人。',
            'category' => 'theme', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'user',
            'theme_required' => true, 'capability_key' => 'balance',
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer']], 'required' => []],
            'callback' => [ThemeManager::class, 'getBalance'],
        ]);
        $r->register('theme_add_balance', [
            'name' => '增加余额', 'description' => '为指定用户增加余额。',
            'category' => 'theme', 'danger' => 'medium', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'theme_required' => true, 'capability_key' => 'balance', 'confirm_mode' => 'single', 'undo_window' => 300,
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer'], 'amount' => ['type' => 'number'], 'reason' => ['type' => 'string']], 'required' => ['user_id', 'amount']],
            'callback' => [ThemeManager::class, 'addBalance'],
        ]);
        $r->register('theme_deduct_balance', [
            'name' => '扣减余额', 'description' => '从指定用户扣减余额。',
            'category' => 'theme', 'danger' => 'medium', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'theme_required' => true, 'capability_key' => 'balance', 'confirm_mode' => 'single', 'undo_window' => 300,
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer'], 'amount' => ['type' => 'number'], 'reason' => ['type' => 'string']], 'required' => ['user_id', 'amount']],
            'callback' => [ThemeManager::class, 'deductBalance'],
        ]);

        // VIP系统
        $r->register('theme_get_vip', [
            'name' => '查询VIP', 'description' => '查询VIP等级和到期时间。不传user_id则查自己，管理员可查任何人。',
            'category' => 'theme', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'user',
            'theme_required' => true, 'capability_key' => 'vip',
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer']], 'required' => []],
            'callback' => [ThemeManager::class, 'getVip'],
        ]);
        $r->register('theme_set_vip', [
            'name' => '设置VIP', 'description' => '设置用户的VIP等级和有效期。',
            'category' => 'theme', 'danger' => 'medium', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'theme_required' => true, 'capability_key' => 'vip', 'confirm_mode' => 'single', 'undo_window' => 600,
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer'], 'level' => ['type' => 'integer'], 'days' => ['type' => 'integer']], 'required' => ['user_id', 'level']],
            'callback' => [ThemeManager::class, 'setVip'],
        ]);
        $r->register('theme_remove_vip', [
            'name' => '移除VIP', 'description' => '移除用户的VIP身份。',
            'category' => 'theme', 'danger' => 'medium', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'theme_required' => true, 'capability_key' => 'vip', 'confirm_mode' => 'single',
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer']], 'required' => ['user_id']],
            'callback' => [ThemeManager::class, 'removeVip'],
        ]);

        // 封禁/黑名单
        $r->register('theme_check_ban', [
            'name' => '检查封禁', 'description' => '检查用户是否被封禁。',
            'category' => 'theme', 'danger' => 'none', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'theme_required' => true, 'capability_key' => 'blacklist',
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer']], 'required' => []],
            'callback' => [ThemeManager::class, 'checkBan'],
        ]);
        $r->register('theme_ban_user', [
            'name' => '封禁用户', 'description' => '封禁指定用户。',
            'category' => 'theme', 'danger' => 'high', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'theme_required' => true, 'capability_key' => 'blacklist', 'confirm_mode' => 'double',
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer'], 'reason' => ['type' => 'string']], 'required' => ['user_id']],
            'callback' => [ThemeManager::class, 'banUser'],
        ]);
        $r->register('theme_unban_user', [
            'name' => '解封用户', 'description' => '解除用户封禁。',
            'category' => 'theme', 'danger' => 'medium', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'theme_required' => true, 'capability_key' => 'blacklist', 'confirm_mode' => 'single',
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer']], 'required' => ['user_id']],
            'callback' => [ThemeManager::class, 'unbanUser'],
        ]);

        // 通知系统
        $r->register('theme_send_notify', [
            'name' => '发送通知', 'description' => '向指定用户发送站内通知。',
            'category' => 'theme', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'theme_required' => true, 'capability_key' => 'notification',
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer'], 'title' => ['type' => 'string'], 'content' => ['type' => 'string']], 'required' => ['user_id', 'title', 'content']],
            'callback' => [ThemeManager::class, 'sendNotify'],
        ]);

        // 签到系统
        $r->register('theme_get_checkin', [
            'name' => '查询签到', 'description' => '查询用户签到状态和连续签到天数。',
            'category' => 'theme', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'user',
            'theme_required' => true, 'capability_key' => 'checkin',
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer']], 'required' => []],
            'callback' => [ThemeManager::class, 'getCheckin'],
        ]);
        $r->register('theme_do_checkin', [
            'name' => '执行签到', 'description' => '执行每日签到操作。',
            'category' => 'theme', 'danger' => 'low', 'wp_cap' => 'read', 'min_role' => 'user',
            'theme_required' => true, 'capability_key' => 'checkin',
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer']], 'required' => []],
            'callback' => [ThemeManager::class, 'doCheckin'],
        ]);

        // 勋章系统
        $r->register('theme_get_medals', [
            'name' => '查询勋章', 'description' => '查询用户拥有的勋章列表。',
            'category' => 'theme', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'user',
            'theme_required' => true, 'capability_key' => 'medal',
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer']], 'required' => []],
            'callback' => [ThemeManager::class, 'getMedals'],
        ]);
        $r->register('theme_add_medal', [
            'name' => '添加勋章', 'description' => '为用户添加勋章。',
            'category' => 'theme', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'theme_required' => true, 'capability_key' => 'medal',
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer'], 'medal_id' => ['type' => 'string']], 'required' => ['user_id', 'medal_id']],
            'callback' => [ThemeManager::class, 'addMedal'],
        ]);
        $r->register('theme_remove_medal', [
            'name' => '移除勋章', 'description' => '移除用户勋章。',
            'category' => 'theme', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'theme_required' => true, 'capability_key' => 'medal',
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer'], 'medal_id' => ['type' => 'string']], 'required' => ['user_id', 'medal_id']],
            'callback' => [ThemeManager::class, 'removeMedal'],
        ]);

        // 订单系统
        $r->register('theme_get_orders', [
            'name' => '查询订单', 'description' => '查询订单列表。不传user_id则查自己，管理员可查任何人。',
            'category' => 'theme', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'user',
            'theme_required' => true, 'capability_key' => 'order',
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer'], 'limit' => ['type' => 'integer', 'default' => 20]], 'required' => []],
            'callback' => [ThemeManager::class, 'getOrders'],
        ]);

        // 邀请系统
        $r->register('theme_get_invites', [
            'name' => '查询邀请', 'description' => '查询用户邀请统计。',
            'category' => 'theme', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'user',
            'theme_required' => true, 'capability_key' => 'invite',
            'schema' => ['type' => 'object', 'properties' => ['user_id' => ['type' => 'integer']], 'required' => []],
            'callback' => [ThemeManager::class, 'getInvites'],
        ]);

        // 批量通知
        $r->register('theme_bulk_notify', [
            'name' => '批量通知', 'description' => '向多个用户批量发送站内通知。user_ids为用户ID数组。',
            'category' => 'theme', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'theme_required' => true, 'capability_key' => 'notification',
            'schema' => ['type' => 'object', 'properties' => [
                'user_ids' => ['type' => 'array', 'description' => '目标用户ID列表'],
                'title'    => ['type' => 'string', 'description' => '通知标题'],
                'content'  => ['type' => 'string', 'description' => '通知内容'],
            ], 'required' => ['user_ids', 'content']],
            'callback' => [ThemeManager::class, 'sendBulkNotify'],
        ]);

        // Webhook通知（支持钉钉/飞书/企业微信）
        $r->register('theme_send_webhook', [
            'name' => 'Webhook通知', 'description' => '通过Webhook向钉钉/飞书/企业微信发送通知。type可选dingtalk/feishu/wecom。',
            'category' => 'theme', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'theme_required' => false,
            'schema' => ['type' => 'object', 'properties' => [
                'webhook_url' => ['type' => 'string', 'description' => 'Webhook URL'],
                'title'       => ['type' => 'string', 'description' => '消息标题'],
                'content'     => ['type' => 'string', 'description' => '消息内容（支持Markdown）'],
                'type'        => ['type' => 'string', 'enum' => ['dingtalk', 'feishu', 'wecom'], 'default' => 'dingtalk'],
            ], 'required' => ['webhook_url', 'content']],
            'callback' => [ThemeManager::class, 'sendWebhook'],
        ]);

        // 论坛增强
        $r->register('theme_forum_plates', [
            'name' => '论坛版块', 'description' => '查看论坛版块列表。',
            'category' => 'theme', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'user',
            'theme_required' => true, 'capability_key' => 'bbs',
            'schema' => ['type' => 'object', 'properties' => [], 'required' => []],
            'callback' => [ThemeManager::class, 'getForumPlates'],
        ]);

        $r->register('theme_forum_search', [
            'name' => '论坛搜索', 'description' => '搜索论坛帖子内容。',
            'category' => 'theme', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'user',
            'theme_required' => true, 'capability_key' => 'bbs',
            'schema' => ['type' => 'object', 'properties' => [
                'keyword' => ['type' => 'string', 'description' => '搜索关键词'],
                'limit'   => ['type' => 'integer', 'default' => 20],
            ], 'required' => ['keyword']],
            'callback' => [ThemeManager::class, 'searchForum'],
        ]);

        $r->register('theme_forum_stats', [
            'name' => '论坛统计', 'description' => '查看论坛统计数据（帖子数/回复数/版块数）。',
            'category' => 'theme', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'user',
            'theme_required' => true, 'capability_key' => 'bbs',
            'schema' => ['type' => 'object', 'properties' => [], 'required' => []],
            'callback' => [ThemeManager::class, 'getForumStats'],
        ]);

        // 配置管理工具 — 管理员在对话中控制所有后台开关

        // 流水线管理
        $r->register('get_pipeline_config', [
            'name' => '查看流水线配置', 'description' => '查看当前流水线配置：同步/异步阶段状态、触发条件、排除规则。',
            'category' => 'config', 'danger' => 'none', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [], 'required' => []],
            'callback' => [ConfigManager::class, 'getPipelineConfig'],
        ]);

        $r->register('toggle_pipeline_stage', [
            'name' => '开关流水线阶段', 'description' => '启用或禁用流水线某个处理阶段。同步: generate_tags/generate_seo/generate_summary/build_autolink/polish_content/push_search/generate_schema/score_seo。异步: generate_cover/generate_alt/localize_image/compress_image/apply_watermark/convert_to_webp。',
            'category' => 'config', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [
                'stage_id' => ['type' => 'string', 'description' => '阶段ID，如 generate_tags'],
                'enabled'  => ['type' => 'boolean', 'description' => 'true=启用, false=禁用'],
            ], 'required' => ['stage_id', 'enabled']],
            'callback' => [ConfigManager::class, 'togglePipelineStage'],
        ]);

        $r->register('toggle_pipeline_trigger', [
            'name' => '开关流水线触发', 'description' => '启用或禁用流水线触发条件。trigger可选: publish（发布时）/update（更新时）/schedule（定时扫描）。',
            'category' => 'config', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [
                'trigger' => ['type' => 'string', 'description' => '触发条件: publish/update/schedule'],
                'enabled' => ['type' => 'boolean', 'description' => 'true=启用, false=禁用'],
            ], 'required' => ['trigger', 'enabled']],
            'callback' => [ConfigManager::class, 'togglePipelineTrigger'],
        ]);

        $r->register('set_pipeline_exclude', [
            'name' => '设置流水线排除', 'description' => '设置流水线排除规则：分类、文章ID、最小字数。',
            'category' => 'config', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [
                'categories' => ['type' => 'string', 'description' => '排除的分类名称或ID，逗号分隔'],
                'post_ids'   => ['type' => 'string', 'description' => '排除的文章ID，逗号分隔'],
                'min_words'  => ['type' => 'integer', 'description' => '最小字数阈值'],
            ], 'required' => []],
            'callback' => [ConfigManager::class, 'setPipelineExclude'],
        ]);

        // 工具注册表
        $r->register('list_tools_status', [
            'name' => '查看工具状态', 'description' => '查看所有工具在各角色（guest/user/vip/admin）下的启用/禁用状态。返回每个工具在每个角色的可用性。',
            'category' => 'config', 'danger' => 'none', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [], 'required' => []],
            'callback' => [ConfigManager::class, 'listToolsStatus'],
        ]);

        // 三级管理工具：单个 / 批量 / 全部
        $r->register('toggle_tool', [
            'name' => '开关工具', 'description' => '对指定角色启用或禁用单个工具。role默认user，tool_id可从list_tools_status获取。',
            'category' => 'config', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [
                'tool_id' => ['type' => 'string', 'description' => '工具ID'],
                'role'    => ['type' => 'string', 'description' => '角色: guest/user/vip/admin，默认user'],
            ], 'required' => ['tool_id']],
            'callback' => [ConfigManager::class, 'toggleTool'],
        ]);

        $r->register('toggle_tool_for_role', [
            'name' => '开关角色工具', 'description' => '对指定角色启用或禁用单个工具（需同时传enabled）。',
            'category' => 'config', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [
                'role'    => ['type' => 'string', 'description' => '角色: guest/user/vip/admin'],
                'tool_id' => ['type' => 'string', 'description' => '工具ID'],
                'enabled' => ['type' => 'boolean', 'description' => 'true=启用, false=禁用'],
            ], 'required' => ['role', 'tool_id', 'enabled']],
            'callback' => [ConfigManager::class, 'toggleToolForRole'],
        ]);

        $r->register('enable_tools_for_role', [
            'name' => '批量启用工具', 'description' => '为指定角色批量启用工具。tool_ids为工具ID数组或逗号分隔字符串。如"给用户开启标签、SEO、摘要"。',
            'category' => 'config', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [
                'role'     => ['type' => 'string', 'description' => '角色: guest/user/vip/admin，默认user'],
                'tool_ids' => ['type' => 'array', 'description' => '工具ID数组，如["generate_tags","generate_seo"]'],
            ], 'required' => ['role', 'tool_ids']],
            'callback' => [ConfigManager::class, 'enableToolsForRole'],
        ]);

        $r->register('disable_tools_for_role', [
            'name' => '批量禁用工具', 'description' => '为指定角色批量禁用工具。tool_ids为工具ID数组或逗号分隔。',
            'category' => 'config', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [
                'role'     => ['type' => 'string', 'description' => '角色'],
                'tool_ids' => ['type' => 'array', 'description' => '工具ID数组'],
            ], 'required' => ['role', 'tool_ids']],
            'callback' => [ConfigManager::class, 'disableToolsForRole'],
        ]);

        $r->register('set_all_tools_for_role', [
            'name' => '全开全关工具', 'description' => '一键为指定角色开启或关闭所有工具。enabled=true全开，false全关。如"给用户开启全部内容工具"。',
            'category' => 'config', 'danger' => 'medium', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'confirm_mode' => 'single',
            'schema' => ['type' => 'object', 'properties' => [
                'role'    => ['type' => 'string', 'description' => '角色: guest/user/vip/admin，默认user'],
                'enabled' => ['type' => 'boolean', 'description' => 'true=全开, false=全关'],
            ], 'required' => ['role', 'enabled']],
            'callback' => [ConfigManager::class, 'setAllToolsForRole'],
        ]);

        // AI 连接池
        $r->register('list_ai_connections', [
            'name' => '查看AI连接', 'description' => '查看所有AI文本和图片连接的状态（厂商/模型/主用/启用）。',
            'category' => 'config', 'danger' => 'none', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [], 'required' => []],
            'callback' => [ConfigManager::class, 'listAiConnections'],
        ]);

        $r->register('toggle_ai_connection', [
            'name' => '开关AI连接', 'description' => '启用或禁用指定AI连接。type=text或image，index为连接序号（从0开始）。',
            'category' => 'config', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [
                'type'    => ['type' => 'string', 'description' => '连接类型: text 或 image'],
                'index'   => ['type' => 'integer', 'description' => '连接序号，从0开始'],
                'enabled' => ['type' => 'boolean', 'description' => 'true=启用, false=禁用'],
            ], 'required' => ['type', 'index', 'enabled']],
            'callback' => [ConfigManager::class, 'toggleAiConnection'],
        ]);

        $r->register('set_primary_connection', [
            'name' => '设为主用连接', 'description' => '将指定AI连接设为主用。type=text或image，index为连接序号。',
            'category' => 'config', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [
                'type'  => ['type' => 'string', 'description' => '连接类型: text 或 image'],
                'index' => ['type' => 'integer', 'description' => '连接序号，从0开始'],
            ], 'required' => ['type', 'index']],
            'callback' => [ConfigManager::class, 'setPrimaryConnection'],
        ]);

        // 速率与费用
        $r->register('get_rate_config', [
            'name' => '查看速率配置', 'description' => '查看各角色的速率限制和月度费用预算。',
            'category' => 'config', 'danger' => 'none', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [], 'required' => []],
            'callback' => [ConfigManager::class, 'getRateConfig'],
        ]);

        $r->register('set_rate_config', [
            'name' => '设置速率限制', 'description' => '设置指定角色的速率限制。role可选guest/user/vip/admin，可设置daily(每日上限)、per_minute(每分钟)、interval(最小间隔秒数)。也可设monthly_budget月度预算和over_budget_action(warn/block)。',
            'category' => 'config', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [
                'role'         => ['type' => 'string', 'description' => '角色: guest/user/vip/admin'],
                'daily'        => ['type' => 'integer', 'description' => '每日上限'],
                'per_minute'   => ['type' => 'integer', 'description' => '每分钟上限'],
                'interval'     => ['type' => 'integer', 'description' => '最小间隔秒数'],
                'monthly_budget' => ['type' => 'number', 'description' => '月度预算（元）'],
                'over_budget_action' => ['type' => 'string', 'description' => '超限行为: warn/block'],
            ], 'required' => ['role']],
            'callback' => [ConfigManager::class, 'setRateConfig'],
        ]);

        // AI 身份与显示
        $r->register('set_ai_identity', [
            'name' => '设置AI身份', 'description' => '设置AI助手的身份：绑定WordPress用户、自定义名称和头像URL。',
            'category' => 'config', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [
                'user_id' => ['type' => 'integer', 'description' => '绑定的WordPress用户ID，0为不绑定'],
                'name'    => ['type' => 'string', 'description' => 'AI显示名称（可选，留空用绑定用户昵称）'],
                'avatar'  => ['type' => 'string', 'description' => 'AI头像URL（可选）'],
            ], 'required' => []],
            'callback' => [ConfigManager::class, 'setAiIdentity'],
        ]);

        $r->register('get_summary_config', [
            'name' => '查看摘要配置', 'description' => '查看AI摘要在前端显示的配置：开关、动画、标题、风格。',
            'category' => 'config', 'danger' => 'none', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [], 'required' => []],
            'callback' => [ConfigManager::class, 'getSummaryConfig'],
        ]);

        $r->register('set_summary_config', [
            'name' => '设置摘要显示', 'description' => '设置AI摘要的前端显示：开关、打字机动画、标题、标签、风格。style可选: glass/neon/minimal/terminal/warm/neumorph/editorial/chat/pixel/sticky/aurora/menghuan/simple/floating，留空为默认。',
            'category' => 'config', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [
                'enabled'   => ['type' => 'boolean', 'description' => '是否显示摘要卡片'],
                'animation' => ['type' => 'boolean', 'description' => '是否启用打字机动画'],
                'title'     => ['type' => 'string', 'description' => '摘要卡片标题'],
                'badge'     => ['type' => 'string', 'description' => '摘要标签文字'],
                'style'     => ['type' => 'string', 'description' => '展示风格: glass/neon/minimal/terminal/warm/neumorph/editorial/chat/pixel/sticky/aurora/menghuan/simple/floating'],
            ], 'required' => []],
            'callback' => [ConfigManager::class, 'setSummaryConfig'],
        ]);

        // 友链配置管理
        $r->register('get_friend_link_config', [
            'name' => '查看友链配置', 'description' => '查看友链监控配置：开关状态、检测时间、丢失告警阈值。',
            'category' => 'config', 'danger' => 'none', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [], 'required' => []],
            'callback' => [ConfigManager::class, 'getFriendLinkConfig'],
        ]);

        $r->register('set_friend_link_config', [
            'name' => '设置友链配置', 'description' => '设置友链监控：开关(monitor_enabled)、检测时间(monitor_hour 0-23)、丢失告警阈值(lost_alert_threshold 连续失败次数)。',
            'category' => 'config', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [
                'monitor_enabled'      => ['type' => 'boolean', 'description' => '是否启用友链自动检测'],
                'monitor_hour'         => ['type' => 'integer', 'description' => '检测时间（0-23点）'],
                'lost_alert_threshold' => ['type' => 'integer', 'description' => '连续检测失败几次后发通知，默认3'],
            ], 'required' => []],
            'callback' => [ConfigManager::class, 'setFriendLinkConfig'],
        ]);

        // 评论自动化配置
        $r->register('get_comment_config', [
            'name' => '查看评论配置', 'description' => '查看评论自动化配置：自动回复开关、回复自动过审、审核严格度、最少字数、外链拦截。',
            'category' => 'config', 'danger' => 'none', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [], 'required' => []],
            'callback' => [ConfigManager::class, 'getCommentConfig'],
        ]);

        $r->register('set_comment_config', [
            'name' => '设置评论配置', 'description' => '设置评论自动化：auto_reply(AI自动回复开关)、auto_approve_reply(回复是否自动过审)、strictness(审核严格度low/medium/high)、min_words(最少字数阈值)、block_urls(拦截含外链评论)。',
            'category' => 'config', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [
                'auto_reply'         => ['type' => 'boolean', 'description' => '是否启用AI自动回复评论'],
                'auto_approve_reply' => ['type' => 'boolean', 'description' => 'AI回复是否自动过审（默认true）'],
                'strictness'         => ['type' => 'string', 'description' => '审核严格度：low/medium/high'],
                'min_words'          => ['type' => 'integer', 'description' => '最少字数阈值，低于此值待审核'],
                'block_urls'         => ['type' => 'boolean', 'description' => '是否拦截含外部链接的评论'],
            ], 'required' => []],
            'callback' => [ConfigManager::class, 'setCommentConfig'],
        ]);

        // Recipe tools
        $r->register('recipe_create', [
            'name' => '创建配方', 'description' => '创建一个AI操作配方（模板），将多个工具组合保存为可复用配方。',
            'category' => 'content', 'danger' => 'none', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'schema' => ['type' => 'object', 'properties' => [
                'name'        => ['type' => 'string', 'description' => '配方名称'],
                'description' => ['type' => 'string', 'description' => '配方描述'],
                'category'    => ['type' => 'string', 'description' => '分类'],
                'steps'       => ['type' => 'array', 'description' => '步骤列表 [{"tool_id":"xxx","params":{}}]'],
                'is_public'   => ['type' => 'boolean', 'description' => '是否公开'],
            ], 'required' => ['name', 'steps']],
            'callback' => [RecipeManager::class, 'create'],
        ]);

        $r->register('recipe_list', [
            'name' => '配方列表', 'description' => '查看可用的AI操作配方列表。',
            'category' => 'content', 'danger' => 'none', 'wp_cap' => 'read', 'min_role' => 'user',
            'schema' => ['type' => 'object', 'properties' => [
                'category' => ['type' => 'string'], 'search' => ['type' => 'string'],
            ], 'required' => []],
            'callback' => [RecipeManager::class, 'list'],
        ]);

        $r->register('recipe_execute', [
            'name' => '执行配方', 'description' => '按配方步骤依次执行所有AI工具。可指定目标文章ID。',
            'category' => 'content', 'danger' => 'medium', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'schema' => ['type' => 'object', 'properties' => [
                'id'             => ['type' => 'integer', 'description' => '配方ID'],
                'target_post_id' => ['type' => 'integer', 'description' => '目标文章ID（可选）'],
            ], 'required' => ['id']],
            'callback' => [RecipeManager::class, 'execute'],
        ]);

        $r->register('recipe_delete', [
            'name' => '删除配方', 'description' => '删除自己的配方。',
            'category' => 'content', 'danger' => 'medium', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'schema' => ['type' => 'object', 'properties' => [
                'id' => ['type' => 'integer', 'description' => '配方ID'],
            ], 'required' => ['id']],
            'callback' => [RecipeManager::class, 'delete'],
        ]);

        // 补充注册：recipe_get, recipe_update, verify_friend_link
        $r->register('recipe_get', [
            'name' => '查看配方', 'description' => '查看单个配方的详细信息（名称、描述、步骤、公开状态等）。',
            'category' => 'content', 'danger' => 'none', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'schema' => ['type' => 'object', 'properties' => [
                'id' => ['type' => 'integer', 'description' => '配方ID'],
            ], 'required' => ['id']],
            'callback' => [RecipeManager::class, 'get'],
        ]);

        $r->register('recipe_update', [
            'name' => '修改配方', 'description' => '修改已有配方的名称、描述、分类、步骤或公开状态。',
            'category' => 'content', 'danger' => 'low', 'wp_cap' => 'edit_posts', 'min_role' => 'user',
            'schema' => ['type' => 'object', 'properties' => [
                'id'          => ['type' => 'integer', 'description' => '配方ID'],
                'name'        => ['type' => 'string', 'description' => '配方名称'],
                'description' => ['type' => 'string', 'description' => '配方描述'],
                'category'    => ['type' => 'string', 'description' => '分类'],
                'steps'       => ['type' => 'array', 'description' => '步骤列表'],
                'is_public'   => ['type' => 'boolean', 'description' => '是否公开'],
            ], 'required' => ['id']],
            'callback' => [RecipeManager::class, 'update'],
        ]);

        $r->register('verify_friend_link', [
            'name' => '验证友链', 'description' => '手动验证指定友链的互链状态，检测对方网站是否仍保留本站链接。',
            'category' => 'seo', 'danger' => 'low', 'wp_cap' => 'manage_options', 'min_role' => 'admin',
            'schema' => ['type' => 'object', 'properties' => [
                'link_id' => ['type' => 'integer', 'description' => '友链ID'],
            ], 'required' => ['link_id']],
            'callback' => [FriendLinkManager::class, 'verifyFriendLink'],
        ]);
    }
}
