// Special video tools — motion control, face swap, avatar
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, resolveVideoInput, resolveAudioInput } from "../upload.js";

export function registerVideoTools(server: McpServer): void {
  // ─── Motion Control (Kling 2.6 Pro) ───
  server.tool(
    "motion_control_kling",
    "Generate video with motion control using Kling 2.6 Pro (200 credits). Control camera/character motion with reference video.",
    {
      prompt: z.string().describe("Text prompt describing the scene"),
      image: z.string().describe("Character/scene image URL or local file path"),
      video: z.string().describe("Motion reference video URL or local file path"),
      negative_prompt: z.string().default("").describe("What to avoid"),
      character_orientation: z.enum(["image", "video"]).default("image").describe("Character orientation source"),
      keep_original_sound: z.boolean().default(false).describe("Keep original audio from reference video"),
      duration: z.number().int().min(3).max(15).default(5),
    },
    async ({ prompt, image, video, negative_prompt, character_orientation, keep_original_sound, duration }) => {
      const [imageUrl, videoUrl] = await Promise.all([
        resolveImageInput(image),
        resolveVideoInput(video),
      ]);
      const ep = TOOL_ENDPOINTS.motion_control_kling;
      const result = await submitAndPoll(ep.endpoint, ep.scenario, {
        prompt: prompt.trim(),
        image: imageUrl,
        video: videoUrl,
        negative_prompt: negative_prompt.trim(),
        character_orientation,
        keep_original_sound,
        duration,
      });
      return { content: [{ type: "text", text: `✅ Motion control video generated: ${result.output}\nTask ID: ${result.taskId}` }] };
    },
  );

  // ─── Video Face Swap ───
  server.tool(
    "video_face_swap",
    "Swap a face in a video (25 credits per 5 seconds). Replace faces in existing videos.",
    {
      face_image: z.string().describe("Face image URL or local file path (the face to insert)"),
      video: z.string().describe("Target video URL or local file path"),
      target_gender: z.enum(["all", "male", "female"]).default("all").describe("Which faces to replace"),
      target_index: z.number().int().default(0).describe("Index of face to replace (0=first detected)"),
      video_duration_seconds: z.number().optional().describe("Duration limit in seconds (optional)"),
    },
    async ({ face_image, video, target_gender, target_index, video_duration_seconds }) => {
      const [faceUrl, videoUrl] = await Promise.all([
        resolveImageInput(face_image),
        resolveVideoInput(video),
      ]);
      const body: Record<string, unknown> = {
        face_image: faceUrl,
        video: videoUrl,
        target_gender,
        target_index,
      };
      if (video_duration_seconds !== undefined) body.video_duration_seconds = video_duration_seconds;

      const ep = TOOL_ENDPOINTS.video_face_swap;
      const result = await submitAndPoll(ep.endpoint, ep.scenario, body);
      return { content: [{ type: "text", text: `✅ Face swap video: ${result.output}\nTask ID: ${result.taskId}` }] };
    },
  );

  // ─── Longcat Avatar ───
  server.tool(
    "longcat_avatar",
    "Generate a talking head avatar video from audio+image (80 credits). Creates a realistic talking avatar.",
    {
      audio: z.string().describe("Audio URL or local file path (speech/narration)"),
      image: z.string().describe("Avatar image URL or local file path (face photo)"),
      resolution: z.enum(["480p", "720p", "1080p"]).default("720p"),
      seed: z.number().int().default(-1),
      duration: z.number().int().min(5).default(10).describe("Duration in seconds (minimum 5)"),
    },
    async ({ audio, image, resolution, seed, duration }) => {
      const [audioUrl, imageUrl] = await Promise.all([
        resolveAudioInput(audio),
        resolveImageInput(image),
      ]);
      const ep = TOOL_ENDPOINTS.longcat_avatar;
      const result = await submitAndPoll(ep.endpoint, ep.scenario, {
        audio: audioUrl,
        image: imageUrl,
        resolution,
        seed: seed === -1 ? -1 : seed,
        duration: Math.max(duration, 5),
      });
      return { content: [{ type: "text", text: `✅ Avatar video generated: ${result.output}\nTask ID: ${result.taskId}` }] };
    },
  );
}
