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

module.exports = {
  data: new SlashCommandBuilder()
    .setName('studio')
    .setDescription('View your AeThex Studio profile and projects')
    .addUserOption(option =>
      option.setName('user')
        .setDescription('User to view (defaults to yourself)')
        .setRequired(false)
    ),

  async execute(interaction, supabase, client) {
    if (!supabase) {
      return interaction.reply({ content: 'Database not configured.', ephemeral: true });
    }

    const target = interaction.options.getUser('user') || interaction.user;
    await interaction.deferReply();

    try {
      const { data: link } = await supabase
        .from('discord_links')
        .select('user_id')
        .eq('discord_id', target.id)
        .maybeSingle();

      if (!link) {
        return interaction.editReply({
          embeds: [
            new EmbedBuilder()
              .setColor(0xff6b6b)
              .setDescription(`${target.id === interaction.user.id ? 'You are' : `${target.tag} is`} not linked to AeThex.`)
          ]
        });
      }

      const { data: profile } = await supabase
        .from('user_profiles')
        .select('username, avatar_url, bio')
        .eq('id', link.user_id)
        .maybeSingle();

      const { data: projects, count: projectCount } = await supabase
        .from('studio_projects')
        .select('id, title, description, created_at, likes', { count: 'exact' })
        .eq('user_id', link.user_id)
        .order('created_at', { ascending: false })
        .limit(5);

      const avatarUrl = profile?.avatar_url && profile.avatar_url.startsWith('http') 
        ? profile.avatar_url 
        : target.displayAvatarURL();
      
      const embed = new EmbedBuilder()
        .setColor(0x7c3aed)
        .setTitle(`${profile?.username || target.tag}'s Studio`)
        .setThumbnail(avatarUrl)
        .setDescription(profile?.bio || 'No bio set')
        .addFields(
          { name: 'Total Projects', value: `${projectCount || 0}`, inline: true }
        )
        .setTimestamp();

      if (projects && projects.length > 0) {
        const projectList = projects.map((p, i) => {
          const likes = p.likes || 0;
          return `**${i + 1}. ${p.title}**\n❤️ ${likes} likes`;
        }).join('\n\n');
        embed.addFields({ name: 'Recent Projects', value: projectList });
      } else {
        embed.addFields({ name: 'Projects', value: 'No projects yet' });
      }

      embed.addFields({
        name: 'Visit Studio',
        value: '[aethex.studio](https://aethex.studio)'
      });

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

    } catch (error) {
      console.error('Studio error:', error);
      await interaction.editReply({ content: 'Failed to fetch studio data.' });
    }
  },
};
