const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');

module.exports = {
  data: new SlashCommandBuilder()
    .setName('avatar')
    .setDescription('Get a user\'s avatar')
    .addUserOption(option =>
      option.setName('user')
        .setDescription('User to get avatar of (defaults to yourself)')
        .setRequired(false)
    ),

  async execute(interaction, supabase, client) {
    const target = interaction.options.getUser('user') || interaction.user;

    const sizes = [128, 256, 512, 1024, 2048];
    const links = sizes.map(size => 
      `[${size}](${target.displayAvatarURL({ size, extension: 'png' })})`
    ).join(' | ');

    const embed = new EmbedBuilder()
      .setColor(0x7c3aed)
      .setTitle(`${target.tag}'s Avatar`)
      .setDescription(`Download: ${links}`)
      .setImage(target.displayAvatarURL({ size: 1024, extension: 'png' }))
      .setTimestamp();

    const member = await interaction.guild.members.fetch(target.id).catch(() => null);
    if (member && member.avatar) {
      embed.setThumbnail(member.displayAvatarURL({ size: 256 }));
      embed.setFooter({ text: 'Thumbnail shows server-specific avatar' });
    }

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