import { MessageFlags } from "discord.js"
import type { ChannelSelectMenuInteraction } from "discord.js"
import { generateDiscoEmbed } from "@/utils/player"
import type { Disco } from "@/types/schemas"
import dbGuild from "@/schemas/guild"
import { t } from "@/utils/i18n"

export const id = "player_disco_channel"
export async function execute(interaction: ChannelSelectMenuInteraction) {
	const channel = interaction.channels.first()
	if (!channel) return interaction.reply({ content: t(interaction.locale, "common.no_channel_selected"), flags: MessageFlags.Ephemeral })

	const guildProfile = await dbGuild.findOne({ guildId: interaction.guild?.id })
	if (!guildProfile) return interaction.reply({ content: t(interaction.locale, "common.database_not_found"), flags: MessageFlags.Ephemeral })

	const dbData = guildProfile.get("guildPlayer.disco") as Disco
	dbData.channelId = channel.id

	guildProfile.set("guildPlayer.disco", dbData)
	guildProfile.markModified("guildPlayer.disco")
	await guildProfile.save().catch(console.error)

	// Générer l'embed mis à jour avec la fonction utilitaire
	const { embed, components } = generateDiscoEmbed(dbData, interaction.client, interaction.guild?.id ?? "", interaction.locale)

	// Mettre à jour l'embed original avec les nouvelles données
	return interaction.update({ content: t(interaction.locale, "common.channel_configured_success"), embeds: [embed], components })
}
