const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { colors, emojis } = require('../../utils/constants');

function getStatusFrench(presence) {
    if (!presence || !presence.status) return { text: 'Hors ligne', emoji: '⚫' };
    switch (presence.status) {
        case 'online': return { text: 'En ligne', emoji: '🟢' };
        case 'idle': return { text: 'Absent', emoji: '🟡' };
        case 'dnd': return { text: 'Ne pas déranger', emoji: '🔴' };
        default: return { text: 'Hors ligne', emoji: '⚫' };
    }
}

module.exports = {
    category: 'info',
    data: new SlashCommandBuilder()
        .setName('userinfo')
        .setDescription('Affiche les informations d\'un utilisateur.')
        .addUserOption(option =>
            option.setName('user')
                .setDescription('L\'utilisateur dont tu veux voir les informations')
                .setRequired(false)),
    async execute(interaction) {
        const target = interaction.options.getUser('user') || interaction.user;
        const member = await interaction.guild.members.fetch(target.id).catch(() => null);

        const embed = new EmbedBuilder()
            .setAuthor({ 
                name: target.displayName, 
                iconURL: target.displayAvatarURL({ dynamic: true }) 
            })
            .setTitle(`${emojis.user} Informations Utilisateur`)
            .setColor(colors.info)
            .setThumbnail(target.displayAvatarURL({ dynamic: true, size: 256 }))
            .addFields(
                { name: `${emojis.id} ID`, value: `\`${target.id}\``, inline: true },
                { name: `${emojis.calendar} Compte créé`, value: `<t:${Math.floor(target.createdTimestamp / 1000)}:F>`, inline: true },
                { name: '🤖 Bot', value: target.bot ? '`Oui`' : '`Non`', inline: true }
            )
            .setFooter({ text: `${interaction.guild.name} • ${interaction.client.user.username}`, iconURL: interaction.client.user.displayAvatarURL() })
            .setTimestamp();

        if (member) {
            const status = getStatusFrench(member.presence);
            const roles = member.roles.cache.filter(r => r.id !== interaction.guild.id);
            const rolesDisplay = roles.size > 0 ? roles.sort((a, b) => b.position - a.position).map(r => r.toString()).slice(0, 10).join(', ') : '`Aucun`';

            embed.addFields(
                { name: '📥 A rejoint le', value: `<t:${Math.floor(member.joinedTimestamp / 1000)}:F>`, inline: true },
                { name: '⏰ Statut', value: `${status.emoji} \`${status.text}\``, inline: true },
                { name: '👑 Rôle le plus élevé', value: member.roles.highest.toString(), inline: true },
                { name: '🎭 Rôles', value: rolesDisplay, inline: false }
            );

            if (member.nickname) {
                embed.addFields({ name: '📝 Surnom', value: `\`${member.nickname}\``, inline: true });
            }

            if (roles.size > 10) {
                embed.addFields({ name: '📊 Total de rôles', value: `\`${roles.size}\``, inline: true });
            }
        }

        if (target.banner) {
            embed.setImage(target.bannerURL({ dynamic: true, size: 1024 }));
        }

        await interaction.reply({ embeds: [embed] });
    },
};