import { EmbedBuilder, MessageFlags, ButtonBuilder, ButtonStyle, ActionRowBuilder } from "discord.js"
import type { ButtonInteraction } from "discord.js"
import type { GuildFbx } from "@/types/schemas"
import dbGuild from "@/schemas/guild"
import { t } from "@/utils/i18n"

export const id = "freebox_refresh_status"
export async function execute(interaction: ButtonInteraction) {
	await interaction.deferUpdate()

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

	const dbData = guildProfile.get("guildFbx") as GuildFbx

	// Reconstruire l'embed de statut actualisé
	const embed = new EmbedBuilder()
		.setTitle(t(interaction.locale, "freebox.status.title"))
		.setColor(dbData.enabled ? 0x00ff00 : 0xff0000)
		.addFields(
			{
				name: t(interaction.locale, "freebox.status.config_section"),
				value: [
					t(interaction.locale, "freebox.status.module_field", { status: dbData.enabled ? t(interaction.locale, "freebox.common.enabled") : t(interaction.locale, "freebox.common.disabled") }),
					t(interaction.locale, "freebox.status.host_field", { value: dbData.host ? `\`${dbData.host}\`` : t(interaction.locale, "freebox.status.host_not_configured") }),
					t(interaction.locale, "freebox.status.token_field", { status: dbData.appToken ? t(interaction.locale, "freebox.status.token_configured") : t(interaction.locale, "freebox.status.token_not_configured") })
				].join('\n'),
				inline: false
			}
		)

	// Informations LCD si disponibles
	if (dbData.lcd) {
		const lcdStatus = dbData.lcd.enabled ? t(interaction.locale, "freebox.common.enabled") : t(interaction.locale, "freebox.common.disabled")
		const botManaged = dbData.lcd.botId ? `<@${dbData.lcd.botId}>` : t(interaction.locale, "freebox.status.timer_no_manager")
		const morningTime = dbData.lcd.morningTime ?? t(interaction.locale, "freebox.status.timer_not_configured")
		const nightTime = dbData.lcd.nightTime ?? t(interaction.locale, "freebox.status.timer_not_configured")
		
		embed.addFields({
			name: t(interaction.locale, "freebox.status.timer_section"),
			value: [
				t(interaction.locale, "freebox.timer.status_field", { status: lcdStatus }),
				t(interaction.locale, "freebox.timer.managed_by", { manager: botManaged }),
				t(interaction.locale, "freebox.timer.morning", { time: morningTime }),
				t(interaction.locale, "freebox.timer.night", { time: nightTime })
			].join('\n'),
			inline: false
		})
	}

	embed.setTimestamp()

	// Reconstruire les boutons
	const buttons = new ActionRowBuilder<ButtonBuilder>()
		.addComponents(
			new ButtonBuilder()
				.setCustomId("freebox_test_connection")
				.setLabel(t(interaction.locale, "freebox.buttons.test_connection"))
				.setEmoji("🔌")
				.setStyle(ButtonStyle.Primary)
				.setDisabled(!dbData.appToken),
			new ButtonBuilder()
				.setCustomId("freebox_lcd_status")
				.setLabel(t(interaction.locale, "freebox.buttons.lcd_status"))
				.setEmoji("💡")
				.setStyle(ButtonStyle.Secondary)
				.setDisabled(!dbData.appToken),
			new ButtonBuilder()
				.setCustomId("freebox_refresh_status")
				.setLabel(t(interaction.locale, "freebox.buttons.refresh_status"))
				.setEmoji("🔄")
				.setStyle(ButtonStyle.Success)
		)

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