///|
/// MCP 服务端:对外只暴露「取一张当前屏幕」和「看采集状态」两个工具。
///
/// 走的是 2026-07-28 的无状态协议:没有 initialize 握手,每个请求自己在
/// `params._meta` 里带 protocolVersion。所以对外只有
/// `handle_request(body) -> body` 这一件事,不需要长连接,也不需要 SSE。
///
/// 这里只管「工具做什么」。类型定义和与 SDK 的对接都在 `mcp_endpoint.mbt`。
const SERVER_NAME : String = "screen-kacha"

///|
const SERVER_VERSION : String = "0.1.0"

///|
const MCP_INSTRUCTIONS : String = "本机屏幕的 HTTP 快照服务。snapshot 返回一张当前屏幕的 JPEG;health 返回采集状态。"

///|
pub fn build_mcp(
  store : Store,
  port : Int32,
  access : Access,
) -> McpEndpoint raise {
  McpEndpoint::build(
    SERVER_NAME,
    SERVER_VERSION,
    instructions=MCP_INSTRUCTIONS,
    tools=[
      McpTool::new(
        "snapshot",
        "取一张当前屏幕的截图,返回 JPEG 图片。region 可只取一块,省 token",
        Json::object({
          "type": "object",
          "properties": Json::object({
            "region": Json::object({
              "type": "string",
              "description": "只取一块:x,y,w,h,四段都是 0..1 的比例或带 % 的百分比,例如 0.1,0.1,0.3,0.2 或 10%,10%,30%,20%",
            }),
            "width": Json::object({
              "type": "integer",
              "minimum": 1,
              "maximum": DST_W_MAX.to_int(),
              "description": "输出宽度(像素),不传就是原宽",
            }),
          }),
        }),
        async fn(args) { snapshot_tool(store, args) },
      ),
      McpTool::new(
        "health",
        "采集状态:帧龄、耗时、失败次数、取帧来源、访问策略",
        Json::object({ "type": "object" }),
        health_handler(store, port, access),
      ),
    ],
  )
}

///|
/// health 的工具处理函数。
///
/// 接口要求 async,这条腿本身没有异步调用。写成普通函数直接落在 async 位置上,
/// 省掉一个「注解没用」的告警 —— 给空体套 async 只是为了让签名对上,那是噪声。
fn health_handler(
  store : Store,
  port : Int32,
  access : Access,
) -> async (Json) -> McpToolResult {
  fn(_args) { McpToolResult::text(health_json(store, port, access)) }
}

///|
/// snapshot 的实现。
///
/// 参数是模型给的,不可信:region 解析失败、width 越界都直接回 isError,
/// 不静默退回整屏 —— 那会让调用方以为裁剪生效了,白烧一遍 token。
async fn snapshot_tool(store : Store, args : Json) -> McpToolResult {
  let region = match arg_node(args, "region") {
    None => Some(None)
    Some(String(v)) =>
      match parse_region(v.to_string()) {
        Some(r) => Some(Some(r))
        None => None
      }
    Some(_) => None
  }
  let region = match region {
    None =>
      return McpToolResult::error(
        "region 看不懂。要四段,每段是 0..1 的比例或百分比,例如 0.1,0.1,0.3,0.2",
      )
    Some(r) => r
  }
  let dst_w = match arg_node(args, "width") {
    None => Some(None)
    Some(Number(v, ..)) =>
      // 只收 1 到 DST_W_MAX 的整数;小数、NaN、Inf、越界都算写错
      if v.is_nan() ||
        v.is_inf() ||
        v < 1.0 ||
        v > DST_W_MAX.to_double() ||
        v != v.to_int().to_double() {
        None
      } else {
        Some(Some(v.to_int()))
      }
    Some(_) => None
  }
  let dst_w = match dst_w {
    None => return McpToolResult::error("width 要在 1 到 4096 之间")
    Some(w) => w
  }
  // 和 HTTP 那条路一样:先按最小间隔取帧,再拿最新的一帧
  store.ensure_request(1500L)
  let frame = match store.bytes() {
    None =>
      return McpToolResult::error(
        "还没有抓到帧,过一秒再试;原因看 health 工具或 /health",
      )
    Some(b) => b
  }
  // 不裁剪也不缩放:直接回原始字节,一次编解码都不做
  if region is None && dst_w is None {
    return McpToolResult::image(@util.b64_encode(frame), mime_type="image/jpeg")
  }
  // 要加工就落一份临时文件:C 侧的图像入口吃路径
  let src = store.next_tmp(".jpg")
  let wrote = try {
    @fs.write_file(src, frame, create_mode=CreateOrTruncate)
    true
  } catch {
    _ => false
  }
  if !wrote {
    return McpToolResult::error("临时文件写不出来")
  }
  let out = store.next_tmp(".jpg")
  let w = match dst_w {
    Some(w) => w
    None => 0
  }
  let ok = match region {
    Some(r) =>
      @image.image_region(
        src,
        false,
        0,
        0,
        r.x,
        r.y,
        r.w,
        r.h,
        w,
        82,
        false,
        out,
      )
    None => @image.jpeg_to_file(src, w, 82, false, out)
  }
  let bytes = Some(@fs.read_file(out).binary()) catch { _ => None }
  match (ok, bytes) {
    (true, Some(b)) =>
      McpToolResult::image(@util.b64_encode(b), mime_type="image/jpeg")
    _ => McpToolResult::error("图像处理失败")
  }
}

///|
/// 取出参数对应的 JSON 节点。**没给**返回 None,给了就原样返回 ——
/// 调用方必须能分清「没给」和「给了个不合法的东西」,否则越界参数会被
/// 当成没给,静默退回整屏。
fn arg_node(args : Json, key : String) -> Json? {
  match args {
    Object(obj) => obj.get(key)
    _ => None
  }
}