import { Events, EmbedBuilder, ChannelType } from "discord.js"
import type { GuildMember } from "discord.js"
import { t, getGuildLocale } from "@/utils/i18n"
import { logConsole } from "@/utils/console"

export const name = Events.GuildMemberAdd
export async function execute(member: GuildMember) {
	if (member.guild.id === "1086577543651524699") {
		// Salon posé tamisé
		const guild = member.guild

		guild.members.fetch().then(async () => {
			let i = 0
			guild.members.cache.forEach(member => { if (!member.user.bot) i++ })

			const channel = guild.channels.cache.get("1091140609139560508")
			if (!channel) return

			await channel.setName("Changement...")
			await channel.setName(`${i} Gens Posés`)
		}).catch(console.error)
	} else if (member.guild.id === "796327643783626782") {
		// Jujul Community
		const guild = member.guild
		if (!guild.members.me) return

		const channel = guild.channels.cache.get("837248593609097237")
		if (!channel || (channel.type !== ChannelType.GuildText && channel.type !== ChannelType.GuildAnnouncement)) {
			logConsole('discordjs', 'guild_member_add', { channelId: '837248593609097237' })
			return
		}

		const guildLocale = await getGuildLocale(guild.id)
		const embed = new EmbedBuilder()
			.setColor(guild.members.me.displayHexColor)
			.setTitle(t(guildLocale, "welcome.title", { username: member.user.username }))
			.setDescription(t(guildLocale, "welcome.description", { memberCount: guild.memberCount.toString() }))
			.setThumbnail(member.user.avatarURL())
			.setTimestamp(new Date())

		return channel.send({ embeds: [embed] })
	}
}
