// Text-to-Video tools — 5 models
import { z } from "zod";
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { submitAndPoll } from "../poller.js";
import { TOOL_ENDPOINTS, SIZE_MAP } from "../types.js";

export function registerTextToVideoTools(server: McpServer): void {
  // ─── Wan 2.6 ───
  server.tool(
    "wan_26_t2v",
    "Generate video from text using Wan 2.6 (200-300 credits). High-quality text-to-video with audio support.",
    {
      prompt: z.string().describe("Text prompt describing the video"),
      negative_prompt: z.string().default("").describe("What to avoid in the video"),
      audio: z.string().default("").describe("Audio URL or description (optional)"),
      aspect_ratio: z.enum(["1:1", "16:9", "3:2", "2:3", "3:4", "4:3", "9:16"]).default("16:9"),
      duration: z.number().int().min(3).max(30).default(10).describe("Video duration in seconds"),
      shot_type: z.string().default("").describe("Camera shot type (optional)"),
      enable_prompt_expansion: z.boolean().default(true).describe("Auto-expand prompt for better results"),
      seed: z.number().int().default(-1),
    },
    async ({ prompt, negative_prompt, audio, aspect_ratio, duration, shot_type, enable_prompt_expansion, seed }) => {
      const ep = TOOL_ENDPOINTS.wan_26_t2v;
      const body: Record<string, unknown> = {
        prompt: prompt.trim(),
        size: SIZE_MAP[aspect_ratio] || SIZE_MAP["16:9"],
        duration,
        enable_prompt_expansion,
        seed: seed === -1 ? -1 : seed,
      };
      if (negative_prompt) body.negative_prompt = negative_prompt.trim();
      if (audio) body.audio = audio.trim();
      if (shot_type) body.shot_type = shot_type;

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

  // ─── LTX-2 Fast ───
  server.tool(
    "ltx2_fast_t2v",
    "Generate video from text using LTX-2 Fast (20 credits). Quick text-to-video with optional audio.",
    {
      prompt: z.string().describe("Text prompt describing the video"),
      duration: z.number().int().min(3).max(15).default(5).describe("Video duration in seconds"),
      generate_audio: z.boolean().default(false).describe("Generate audio track"),
    },
    async ({ prompt, duration, generate_audio }) => {
      const ep = TOOL_ENDPOINTS.ltx2_fast_t2v;
      const result = await submitAndPoll(ep.endpoint, ep.scenario, {
        prompt: prompt.trim(),
        duration,
        generate_audio,
      });
      return { content: [{ type: "text", text: `✅ Video generated: ${result.output}\nTask ID: ${result.taskId}` }] };
    },
  );

  // ─── Veo 3.1 ───
  server.tool(
    "veo_31_t2v",
    "Generate video from text using Google Veo 3.1 (200 credits). Google's high-quality text-to-video.",
    {
      prompt: z.string().describe("Text prompt describing the video"),
      aspect_ratio: z.enum(["16:9", "9:16", "1:1"]).default("16:9"),
      resolution: z.enum(["720p", "1080p"]).default("720p"),
      duration: z.number().int().min(3).max(30).default(8).describe("Video duration in seconds"),
      generate_audio: z.boolean().default(true).describe("Generate audio track"),
      camera_fixed: z.boolean().default(false).describe("Keep camera fixed/static"),
      seed: z.number().int().default(-1),
    },
    async ({ prompt, aspect_ratio, resolution, duration, generate_audio, camera_fixed, seed }) => {
      const ep = TOOL_ENDPOINTS.veo_31_t2v;
      const result = await submitAndPoll(ep.endpoint, ep.scenario, {
        prompt: prompt.trim(),
        aspect_ratio,
        resolution,
        duration,
        generate_audio,
        camera_fixed,
        seed: seed === -1 ? -1 : seed,
      });
      return { content: [{ type: "text", text: `✅ Video generated: ${result.output}\nTask ID: ${result.taskId}` }] };
    },
  );

  // ─── Seedance 1.5 Pro ───
  server.tool(
    "seedance_15_pro_t2v",
    "Generate video from text using Seedance 1.5 Pro (100 credits). ByteDance's text-to-video.",
    {
      prompt: z.string().describe("Text prompt describing the video"),
      aspect_ratio: z.enum(["16:9", "9:16", "1:1"]).default("16:9"),
      duration: z.number().int().min(3).max(15).default(5),
      resolution: z.enum(["720p", "1080p"]).default("720p"),
      generate_audio: z.boolean().default(false),
      camera_fixed: z.boolean().default(false),
      seed: z.number().int().default(-1),
    },
    async ({ prompt, aspect_ratio, duration, resolution, generate_audio, camera_fixed, seed }) => {
      const ep = TOOL_ENDPOINTS.seedance_15_pro_t2v;
      const result = await submitAndPoll(ep.endpoint, ep.scenario, {
        prompt: prompt.trim(),
        aspect_ratio,
        duration,
        resolution,
        generate_audio,
        camera_fixed,
        seed: seed === -1 ? -1 : seed,
      });
      return { content: [{ type: "text", text: `✅ Video generated: ${result.output}\nTask ID: ${result.taskId}` }] };
    },
  );

  // ─── PixVerse v5.6 ───
  server.tool(
    "pixverse_56_t2v",
    "Generate video from text using PixVerse v5.6 (100 credits). Creative text-to-video with thinking mode.",
    {
      prompt: z.string().describe("Text prompt describing the video"),
      negative_prompt: z.string().default("").describe("What to avoid"),
      duration: z.number().int().min(3).max(15).default(10),
      generate_audio: z.boolean().default(false).describe("Generate audio track"),
      resolution: z.enum(["720p", "1080p"]).default("720p"),
      resolution_ratio: z.enum(["16:9", "9:16", "1:1"]).default("16:9"),
      thinking_type: z.string().default("").describe("Thinking mode type (optional)"),
      seed: z.number().int().default(-1),
    },
    async ({ prompt, negative_prompt, duration, generate_audio, resolution, resolution_ratio, thinking_type, seed }) => {
      const ep = TOOL_ENDPOINTS.pixverse_56_t2v;
      const body: Record<string, unknown> = {
        prompt: prompt.trim(),
        duration,
        generate_audio_switch: generate_audio,
        resolution,
        resolution_ratio,
        seed,
      };
      if (negative_prompt) body.negative_prompt = negative_prompt.trim();
      if (thinking_type) body.thinking_type = thinking_type;

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