import { SlashCommandBuilder } from "discord.js"
import type { ChatInputCommandInteraction, GuildMember } from "discord.js"
import { getVoiceConnection, joinVoiceChannel } from "@discordjs/voice"
import { t } from "@/utils/i18n"

export const data = new SlashCommandBuilder()
	.setName("papa")
	.setDescription("If daddy calls me, I join him")
	.setDescriptionLocalizations({ fr: "Si papa m'appelle, je le rejoins" })

export async function execute(interaction: ChatInputCommandInteraction) {
	if (interaction.user.id !== "223831938346123275") return interaction.reply({ content: t(interaction.locale, "salonpostam.papa.not_your_father") })

	const guild = interaction.guild
	if (!guild) return interaction.reply({ content: t(interaction.locale, "salonpostam.papa.no_dm") })

	const member = interaction.member as GuildMember

	const botChannel = guild.members.me?.voice.channel
	const papaChannel = member.voice.channel

	if (!papaChannel && botChannel) {
		const voiceConnection = getVoiceConnection(guild.id)
		if (voiceConnection) voiceConnection.destroy()
		return interaction.reply({ content: t(interaction.locale, "salonpostam.papa.leaving_voice") })
	} else if (papaChannel && (!botChannel || botChannel.id !== papaChannel.id)) {
		joinVoiceChannel({
			channelId: papaChannel.id,
			guildId: papaChannel.guild.id,
			adapterCreator: papaChannel.guild.voiceAdapterCreator,
		})
		return interaction.reply({ content: t(interaction.locale, "salonpostam.papa.joining_voice") })
	} else return interaction.reply({ content: t(interaction.locale, "salonpostam.papa.already_connected") })
}
