const API = require("../../lib/API")
const MCP = require("../../lib/Lepao/Mcp.js").MCP

class McpRpc extends API {
    constructor() {
        super()

        this.noEncrypt()
        this.setPath('/mcp')
        this.setMethod('POST')
    }

    async onRequest(req, res) {
        const { method, params = {}, id: req_id } = req.body

        try {
            let result

            if (method === 'initialize') {
                result = {
                    protocolVersion: "2024-11-05",
                    capabilities: { "tools": {} },
                    serverInfo: { name: "runforge", version: "1.0" }
                }
            } else if (method === 'tools/list') {
                result = {
                    "tools": [
                        {
                            "name": "bind_account",
                            "description": "Bind a user account when the user provides a bind code",
                            "inputSchema": {
                                "type": "object",
                                "properties": {
                                    "sender": {
                                        "type": "string",
                                        "description": "Unique user identifier from the chat platform"
                                    },
                                    "bind_code": {
                                        "type": "string",
                                        "description": "Binding code provided by the user"
                                    }
                                },
                                "required": ["sender", "bind_code"]
                            }
                        },
                        {
                            "name": "unbind_account",
                            "description": "Unbind the currently linked user account",
                            "inputSchema": {
                                "type": "object",
                                "properties": {
                                    "sender": {
                                        "type": "string",
                                        "description": "Unique user identifier from the chat platform"
                                    }
                                },
                                "required": ["sender"]
                            }
                        },
                        {
                            "name": "get_account_info",
                            "description": "Retrieve user account information such as name, student ID, account status, and running statistics",
                            "inputSchema": {
                                "type": "object",
                                "properties": {
                                    "sender": {
                                        "type": "string",
                                        "description": "Unique user identifier from the chat platform"
                                    }
                                },
                                "required": ["sender"]
                            }
                        },
                        {
                            "name": "set_notification",
                            "description": "Set notification preference when the user wants to enable or disable notifications",
                            "inputSchema": {
                                "type": "object",
                                "properties": {
                                    "sender": {
                                        "type": "string",
                                        "description": "Unique user identifier from the chat platform"
                                    },
                                    "mode": {
                                        "type": "string",
                                        "enum": ["bot", "email", "none"],
                                        "description": "Notification mode: bot (chat bot), email (email notification), none (disable notifications)"
                                    }
                                },
                                "required": ["sender", "mode"]
                            }
                        },
                        {
                            "name": "change_email",
                            "description": "Update the user's notification email address",
                            "inputSchema": {
                                "type": "object",
                                "properties": {
                                    "sender": {
                                        "type": "string",
                                        "description": "Unique user identifier from the chat platform"
                                    },
                                    "email": {
                                        "type": "string",
                                        "format": "email",
                                        "description": "New email address for notifications"
                                    }
                                },
                                "required": ["sender", "email"]
                            }
                        },
                        {
                            "name": "create_work_order",
                            "description": "Create a customer support ticket when the user reports a problem or suggestion",
                            "inputSchema": {
                                "type": "object",
                                "properties": {
                                    "sender": {
                                        "type": "string",
                                        "description": "Unique user identifier from the chat platform"
                                    },
                                    "email": {
                                        "type": "string",
                                        "format": "email",
                                        "description": "User contact email"
                                    },
                                    "title": {
                                        "type": "string",
                                        "maxLength": 20,
                                        "description": "Short title of the issue (max 20 characters)"
                                    },
                                    "content": {
                                        "type": "string",
                                        "maxLength": 100,
                                        "description": "Detailed description of the issue (max 100 characters)"
                                    }
                                },
                                "required": ["sender", "email", "title", "content"]
                            }
                        },
                        {
                            "name": "change_area",
                            "description": "Set the running area for the user. If the user requests a random area, use '随机分配'",
                            "inputSchema": {
                                "type": "object",
                                "properties": {
                                    "sender": {
                                        "type": "string",
                                        "description": "Unique user identifier from the chat platform"
                                    },
                                    "area": {
                                        "type": "string",
                                        "enum": [
                                            "兰花湖校区跑区",
                                            "主校区北跑区",
                                            "主校区南跑区",
                                            "重庆工商大学茶园校区",
                                            "随机分配"
                                        ],
                                        "description": "Running area. Must be one of the predefined areas, or '随机分配' for random assignment"
                                    }
                                },
                                "required": ["sender", "area"]
                            }
                        },
                        {
                            "name": "change_auto_time",
                            "description": "Set the automatic running start hour. Use -1 if the user wants a random time assigned",
                            "inputSchema": {
                                "type": "object",
                                "properties": {
                                    "sender": {
                                        "type": "string",
                                        "description": "Unique user identifier from the chat platform"
                                    },
                                    "auto_time": {
                                        "type": "integer",
                                        "minimum": -1,
                                        "maximum": 23,
                                        "description": "Hour of day to start running (7-23), or -1 to assign a random time"
                                    }
                                },
                                "required": ["sender", "auto_time"]
                            }
                        },
                        {
                            "name": "change_auto_day",
                            "description": "Set the days of the week for automatic running",
                            "inputSchema": {
                                "type": "object",
                                "properties": {
                                    "sender": {
                                        "type": "string",
                                        "description": "Unique user identifier from the chat platform"
                                    },
                                    "auto_day": {
                                        "type": "array",
                                        "items": {
                                            "type": "integer",
                                            "minimum": 0,
                                            "maximum": 6
                                        },
                                        "description": "Days of week to run (0=Sunday, 6=Saturday)"
                                    }
                                },
                                "required": ["sender", "auto_day"]
                            }
                        }
                    ]
                }
            } else if (method === 'tools/call') {
                const { name, arguments: args = {} } = params

                let output

                switch (name) {
                    case "bind_account":
                        output = await MCP.bind_account(args)
                        break
                    case "get_account_info":
                        output = await MCP.get_account_info(args)
                        break
                    case "unbind_account":
                        output = await MCP.unbind_account(args)
                        break
                    case "set_notification":
                        output = await MCP.set_notification(args)
                        break
                    case "change_email":
                        output = await MCP.change_email(args)
                        break
                    case "create_work_order":
                        output = await MCP.create_work_order(args)
                        break
                    case "change_area":
                        output = await MCP.change_area(args)
                        break
                    case "change_auto_time":
                        output = await MCP.change_auto_time(args)
                        break
                    case "change_auto_day":
                        output = await MCP.change_auto_day(args)
                        break
                    default:
                        output = "未知工具"
                }

                result = { content: [{ type: "text", text: output }] }
            } else {
                result = { error: `未知方法: ${method}` }
            }

            // 返回标准 JSON-RPC 响应
            return res.json({
                jsonrpc: "2.0",
                id: req_id ?? null,
                result
            })
        } catch (e) {
            return res.json({
                jsonrpc: "2.0",
                id: req_id ?? null,
                error: { code: -32000, message: e.message }
            })
        }
    }
}

module.exports.McpRpc = McpRpc