import { SlashCommandBuilder } from "discord.js";
import type { CommandModule } from "../../discord/types.js";
import { translateService } from "../../services/translate/translate-service.js";

export const translateCommand: CommandModule = {
  data: new SlashCommandBuilder()
    .setName("translate")
    .setDescription("텍스트를 번역합니다 (DeepL 우선, 실패 시 Google Web v2)")
    .addStringOption((opt) => opt.setName("text").setDescription("번역할 텍스트").setRequired(true))
    .addStringOption((opt) =>
      opt
        .setName("source")
        .setDescription("원문 언어 (예: auto, ja, en)")
        .setRequired(false),
    )
    .addStringOption((opt) =>
      opt
        .setName("target")
        .setDescription("목표 언어 (예: ko, en, ja)")
        .setRequired(false),
    ),
  async execute(interaction) {
    const text = interaction.options.getString("text", true);
    const source = interaction.options.getString("source") || "auto";
    const target = interaction.options.getString("target") || "ko";

    await interaction.deferReply();
    try {
      const result = await translateService.translate(text, source, target);
      const header = `**번역 완료** engine=\`${result.engine}\` chars=${result.inputChars} chunks=${result.chunkCount}`;
      await interaction.editReply(`${header}\n\n${result.translatedText}`);
    } catch (error) {
      await interaction.editReply(
        `번역 실패: ${error instanceof Error ? error.message : "알 수 없는 오류"}`,
      );
    }
  },
};
