// Utility tools — prompt enhance, estimate credits, file uploads
import { z } from "zod";
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { getClient } from "../client.js";
import { uploadImage, uploadVideo, uploadAudio } from "../upload.js";

export function registerUtilityTools(server: McpServer): void {
  // ─── Prompt Enhance ───
  server.tool(
    "prompt_enhance",
    "Enhance/expand a text prompt for better AI image/video generation results.",
    {
      text: z.string().describe("The prompt text to enhance"),
      mode: z.enum(["image", "video"]).default("image").describe("Optimize for image or video generation"),
      image: z.string().default("").describe("Optional reference image URL for context"),
    },
    async ({ text, mode, image }) => {
      const client = getClient();
      const body: Record<string, unknown> = { text: text.trim(), mode };
      if (image) body.image = image;
      const data = await client.post("/api/ai/prompt-enhance", body);
      return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
    },
  );

  // ─── Estimate Credits ───
  server.tool(
    "estimate_credits",
    "Estimate the credit cost before running an AI generation task.",
    {
      apiKey: z.string().describe("The endpoint/model key (e.g., 'generate', 'text-to-video-wan-2-6')"),
      params: z.record(z.string(), z.unknown()).default({}).describe("The generation parameters to estimate cost for"),
    },
    async ({ apiKey, params }) => {
      const client = getClient();
      const data = await client.post("/api/ai/estimate-credits", { apiKey, params });
      return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
    },
  );

  // ─── Upload Image ───
  server.tool(
    "upload_image",
    "Upload a local image file to z-image.ai storage. Returns a URL for use in image-to-image, video, and other tools.",
    {
      file_path: z.string().describe("Local file path to the image"),
    },
    async ({ file_path }) => {
      const url = await uploadImage(file_path);
      return { content: [{ type: "text", text: `✅ Image uploaded: ${url}` }] };
    },
  );

  // ─── Upload Video ───
  server.tool(
    "upload_video",
    "Upload a local video file to z-image.ai storage. Returns a URL for use in face swap, motion control, etc.",
    {
      file_path: z.string().describe("Local file path to the video"),
    },
    async ({ file_path }) => {
      const url = await uploadVideo(file_path);
      return { content: [{ type: "text", text: `✅ Video uploaded: ${url}` }] };
    },
  );

  // ─── Upload Audio ───
  server.tool(
    "upload_audio",
    "Upload a local audio file to z-image.ai storage. Returns a URL for use in avatar, video generation, etc.",
    {
      file_path: z.string().describe("Local file path to the audio"),
    },
    async ({ file_path }) => {
      const url = await uploadAudio(file_path);
      return { content: [{ type: "text", text: `✅ Audio uploaded: ${url}` }] };
    },
  );
}
