// Image utility tools — eraser, background remover, upscaler, layered decomposition
import { z } from "zod";
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { submitAndPoll } from "../poller.js";
import { TOOL_ENDPOINTS } from "../types.js";
import { resolveImageInput } from "../upload.js";

export function registerImageUtilTools(server: McpServer): void {
  // ─── Image Eraser ───
  server.tool(
    "image_eraser",
    "Erase/remove objects from an image using AI (10 credits). Describe what to remove.",
    {
      image: z.string().describe("Image URL or local file path"),
      prompt: z.string().default("").describe("Description of what to erase (optional — auto-detect if empty)"),
      output_format: z.enum(["png", "jpeg", "webp"]).default("jpeg"),
    },
    async ({ image, prompt, output_format }) => {
      const imageUrl = await resolveImageInput(image);
      const ep = TOOL_ENDPOINTS.image_eraser;
      const result = await submitAndPoll(ep.endpoint, ep.scenario, {
        image: imageUrl,
        prompt: prompt.trim(),
        output_format,
      });
      return { content: [{ type: "text", text: `✅ Objects erased: ${result.output}\nTask ID: ${result.taskId}` }] };
    },
  );

  // ─── Background Remover ───
  server.tool(
    "background_remover",
    "Remove the background from an image (2 credits). Returns PNG with transparent background.",
    {
      image: z.string().describe("Image URL or local file path"),
    },
    async ({ image }) => {
      const imageUrl = await resolveImageInput(image);
      const ep = TOOL_ENDPOINTS.background_remover;
      const result = await submitAndPoll(ep.endpoint, ep.scenario, {
        image: imageUrl,
        enable_base64_output: false,
        enable_sync_mode: false,
      });
      return { content: [{ type: "text", text: `✅ Background removed: ${result.output}\nTask ID: ${result.taskId}` }] };
    },
  );

  // ─── Image Upscaler ───
  server.tool(
    "image_upscaler",
    "Upscale/enhance image resolution using AI (4 credits). Increase image quality and detail.",
    {
      image: z.string().describe("Image URL or local file path"),
      target_resolution: z.enum(["2x", "4x"]).default("2x").describe("Upscale factor"),
      output_format: z.enum(["png", "jpeg", "webp"]).default("jpeg"),
    },
    async ({ image, target_resolution, output_format }) => {
      const imageUrl = await resolveImageInput(image);
      const ep = TOOL_ENDPOINTS.image_upscaler;
      const result = await submitAndPoll(ep.endpoint, ep.scenario, {
        image: imageUrl,
        enable_base64_output: false,
        enable_sync_mode: false,
        output_format,
        target_resolution,
      });
      return { content: [{ type: "text", text: `✅ Image upscaled: ${result.output}\nTask ID: ${result.taskId}` }] };
    },
  );

  // ─── Qwen Image Layered ───
  server.tool(
    "image_layered",
    "Decompose an image into semantic layers using Qwen (20 credits/layer). Separates objects/background.",
    {
      image: z.string().describe("Image URL or local file path"),
      num_layers: z.number().int().min(2).max(10).default(3).describe("Number of layers to decompose into (min 2)"),
    },
    async ({ image, num_layers }) => {
      const imageUrl = await resolveImageInput(image);
      const ep = TOOL_ENDPOINTS.image_layered;
      const result = await submitAndPoll(ep.endpoint, ep.scenario, {
        image: imageUrl,
        num_layers: Math.max(2, num_layers),
      });
      return { content: [{ type: "text", text: `✅ Image decomposed into ${num_layers} layers: ${result.output}\nTask ID: ${result.taskId}` }] };
    },
  );
}
