import { MessageFlags } from "discord.js"
import type { ButtonInteraction } 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_disable"
export async function execute(interaction: ButtonInteraction) {
	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.enabled = false

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

	// Utiliser la fonction utilitaire pour générer l'embed et les composants mis à jour
	const { embed, components } = generateDiscoEmbed(dbData, interaction.client, interaction.guild?.id ?? "", interaction.locale)

	return interaction.update({ embeds: [embed], components: components })
}
