import { SlashCommandBuilder } from "discord.js"
import type { ChatInputCommandInteraction } from "discord.js"
import { useQueue } from "discord-player"
import type { QueueRepeatMode } from "discord-player"
import { t } from "@/utils/i18n"

export const data = new SlashCommandBuilder()
	.setName("loop")
	.setDescription("Loop the current music")
	.setNameLocalizations({ fr: "boucle" })
	.setDescriptionLocalizations({ fr: "Boucler la musique en cours de lecture" })
	.addIntegerOption(option => option
		.setName("mode")
		.setDescription("Loop mode (0 = Off | 1 = Track | 2 = Queue | 3 = Autoplay)")
		.setDescriptionLocalizations({ fr: "Mode de boucle (0 = Arrêt | 1 = Titre | 2 = File d'Attente | 3 = Autoplay)" })
		.setRequired(true)
		.setMinValue(0)
		.setMaxValue(3)
	)

export async function execute(interaction: ChatInputCommandInteraction) {
	const mode = interaction.options.getInteger("mode", true)
	const queue = useQueue(interaction.guild?.id ?? "")
	if (!queue) return interaction.followUp({ content: t(interaction.locale, "player.no_queue") })

	queue.setRepeatMode(mode as QueueRepeatMode)

	return interaction.reply(t(interaction.locale, mode === 0 ? "player.loop_off" : mode === 1 ? "player.loop_track" : mode === 2 ? "player.loop_queue" : "player.loop_autoplay"))
}
