import { SlashCommandBuilder } from "discord.js";
import type { CommandModule } from "../../discord/types.js";
import { musicPlayer } from "../../services/music/music-player.js";

export const playCommand: CommandModule = {
  data: new SlashCommandBuilder()
    .setName("play")
    .setDescription("유튜브 URL 또는 검색어를 재생합니다")
    .addStringOption((opt) => opt.setName("query").setDescription("URL 또는 검색어").setRequired(true)),
  async execute(interaction) {
    if (!interaction.guildId) return;
    const query = interaction.options.getString("query", true);
    await interaction.deferReply();
    try {
      const message = await musicPlayer.enqueueFromInteraction(interaction, query);
      await interaction.editReply(message);
    } catch (error) {
      await interaction.editReply(
        `재생 실패: ${error instanceof Error ? error.message : "알 수 없는 오류"}`,
      );
    }
  },
};
