const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, StringSelectMenuBuilder } = require("discord.js");

module.exports = {
  data: new SlashCommandBuilder()
    .setName("help")
    .setDescription("View all AeThex bot commands and features")
    .addStringOption(option =>
      option.setName('category')
        .setDescription('Specific category to view')
        .setRequired(false)
        .addChoices(
          { name: '🔗 Account', value: 'account' },
          { name: '⚔️ Realms', value: 'realms' },
          { name: '📊 Community', value: 'community' },
          { name: '⭐ Leveling', value: 'leveling' },
          { name: '🛡️ Moderation', value: 'moderation' },
          { name: '🔧 Utility', value: 'utility' },
          { name: '⚙️ Admin', value: 'admin' }
        )
    ),

  async execute(interaction) {
    const category = interaction.options.getString('category');

    if (category) {
      const embed = getCategoryEmbed(category);
      return interaction.reply({ embeds: [embed], ephemeral: true });
    }

    const mainEmbed = new EmbedBuilder()
      .setColor(0x7c3aed)
      .setAuthor({ 
        name: 'AeThex Bot Help',
        iconURL: interaction.client.user.displayAvatarURL()
      })
      .setDescription('Welcome to AeThex Bot! Select a category below to view commands.')
      .addFields(
        {
          name: "🔗 Account",
          value: "`/verify` `/unlink` `/profile`",
          inline: true,
        },
        {
          name: "⚔️ Realms",
          value: "`/set-realm` `/verify-role` `/refresh-roles`",
          inline: true,
        },
        {
          name: "📊 Community",
          value: "`/stats` `/leaderboard` `/post` `/poll`",
          inline: true,
        },
        {
          name: "⭐ Leveling",
          value: "`/rank` `/daily` `/badges`",
          inline: true,
        },
        {
          name: "🛡️ Moderation",
          value: "`/warn` `/kick` `/ban` `/timeout` `/modlog`",
          inline: true,
        },
        {
          name: "🔧 Utility",
          value: "`/userinfo` `/serverinfo` `/avatar`",
          inline: true,
        },
        {
          name: "⚙️ Admin",
          value: "`/config` `/announce` `/embed` `/rolepanel`",
          inline: true,
        },
        {
          name: "🎉 Fun",
          value: "`/giveaway` `/schedule`",
          inline: true,
        },
        {
          name: "🛡️ Auto-Mod",
          value: "`/automod`",
          inline: true,
        }
      )
      .addFields({
        name: "🔗 Quick Links",
        value: "[AeThex Platform](https://aethex.dev) • [Creator Directory](https://aethex.dev/creators) • [Community Feed](https://aethex.dev/community/feed)",
        inline: false,
      })
      .setFooter({ 
        text: "Use /help [category] for detailed commands • AeThex Bot",
        iconURL: interaction.client.user.displayAvatarURL()
      })
      .setTimestamp();

    const selectMenu = new StringSelectMenuBuilder()
      .setCustomId('help_category')
      .setPlaceholder('Select a category for details...')
      .addOptions([
        { label: 'Account', description: 'Link and manage your account', emoji: '🔗', value: 'account' },
        { label: 'Realms', description: 'Realm selection and roles', emoji: '⚔️', value: 'realms' },
        { label: 'Community', description: 'Community features', emoji: '📊', value: 'community' },
        { label: 'Leveling', description: 'XP and leveling system', emoji: '⭐', value: 'leveling' },
        { label: 'Moderation', description: 'Moderation tools', emoji: '🛡️', value: 'moderation' },
        { label: 'Utility', description: 'Utility commands', emoji: '🔧', value: 'utility' },
        { label: 'Admin', description: 'Admin and config', emoji: '⚙️', value: 'admin' },
      ]);

    const row = new ActionRowBuilder().addComponents(selectMenu);

    await interaction.reply({ embeds: [mainEmbed], components: [row], ephemeral: true });
  },
};

function getCategoryEmbed(category) {
  const categories = {
    account: {
      title: '🔗 Account Commands',
      color: 0x3b82f6,
      commands: [
        { name: '/verify', desc: 'Link your Discord account to AeThex' },
        { name: '/unlink', desc: 'Disconnect your Discord from AeThex' },
        { name: '/profile [@user]', desc: 'View your or another user\'s AeThex profile' },
      ]
    },
    realms: {
      title: '⚔️ Realm Commands',
      color: 0xf97316,
      commands: [
        { name: '/set-realm', desc: 'Choose your primary realm (Labs, GameForge, Corp, Foundation, Dev-Link)' },
        { name: '/verify-role', desc: 'Check your assigned Discord roles' },
        { name: '/refresh-roles', desc: 'Sync your roles based on AeThex profile' },
      ]
    },
    community: {
      title: '📊 Community Commands',
      color: 0x22c55e,
      commands: [
        { name: '/stats', desc: 'View your AeThex statistics and activity' },
        { name: '/leaderboard [category]', desc: 'See the top contributors' },
        { name: '/post', desc: 'Create a post in the AeThex community feed' },
        { name: '/poll', desc: 'Create a community poll' },
        { name: '/studio [@user]', desc: 'View AeThex Studio profile' },
        { name: '/foundation [@user]', desc: 'View Foundation contributions' },
      ]
    },
    leveling: {
      title: '⭐ Leveling Commands',
      color: 0xfbbf24,
      commands: [
        { name: '/rank [@user]', desc: 'View your level and unified XP' },
        { name: '/daily', desc: 'Claim your daily XP bonus (+50 base + streak)' },
        { name: '/badges', desc: 'View earned badges across platforms' },
      ]
    },
    moderation: {
      title: '🛡️ Moderation Commands',
      color: 0xef4444,
      commands: [
        { name: '/warn @user [reason]', desc: 'Warn a user' },
        { name: '/kick @user [reason]', desc: 'Kick a user from the server' },
        { name: '/ban @user [reason]', desc: 'Ban a user from the server' },
        { name: '/timeout @user [minutes] [reason]', desc: 'Timeout a user' },
        { name: '/modlog @user', desc: 'View a user\'s moderation history' },
        { name: '/auditlog', desc: 'View admin action history' },
      ]
    },
    utility: {
      title: '🔧 Utility Commands',
      color: 0x8b5cf6,
      commands: [
        { name: '/userinfo [@user]', desc: 'View detailed user information' },
        { name: '/serverinfo', desc: 'View server statistics and info' },
        { name: '/avatar [@user]', desc: 'Get a user\'s avatar' },
        { name: '/status', desc: 'View network status' },
      ]
    },
    admin: {
      title: '⚙️ Admin Commands',
      color: 0x6b7280,
      commands: [
        { name: '/config', desc: 'View and edit server configuration' },
        { name: '/announce', desc: 'Send cross-server announcements' },
        { name: '/embed', desc: 'Create custom embed messages' },
        { name: '/rolepanel', desc: 'Create role button panels' },
        { name: '/giveaway', desc: 'Create and manage giveaways' },
        { name: '/schedule', desc: 'Schedule messages for later' },
        { name: '/automod', desc: 'Configure auto-moderation' },
        { name: '/admin', desc: 'Bot administration commands' },
        { name: '/federation', desc: 'Manage cross-server role sync' },
        { name: '/ticket', desc: 'Manage support tickets' },
      ]
    }
  };

  const cat = categories[category] || categories.account;

  return new EmbedBuilder()
    .setColor(cat.color)
    .setTitle(cat.title)
    .setDescription(cat.commands.map(c => `**${c.name}**\n${c.desc}`).join('\n\n'))
    .setFooter({ text: 'Use /help to see all categories' })
    .setTimestamp();
}

module.exports.getCategoryEmbed = getCategoryEmbed;
