const { 
  SlashCommandBuilder, 
  EmbedBuilder, 
  PermissionFlagsBits,
  ModalBuilder,
  TextInputBuilder,
  TextInputStyle,
  ActionRowBuilder,
  ChannelType
} = require('discord.js');

module.exports = {
  data: new SlashCommandBuilder()
    .setName('embed')
    .setDescription('Create a custom embed message')
    .setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages)
    .addChannelOption(option =>
      option.setName('channel')
        .setDescription('Channel to send the embed to')
        .setRequired(true)
        .addChannelTypes(ChannelType.GuildText, ChannelType.GuildAnnouncement)
    )
    .addStringOption(option =>
      option.setName('color')
        .setDescription('Embed color')
        .setRequired(false)
        .addChoices(
          { name: '🟣 Purple (Default)', value: '7c3aed' },
          { name: '🟢 Green (Success)', value: '22c55e' },
          { name: '🔴 Red (Alert)', value: 'ef4444' },
          { name: '🔵 Blue (Info)', value: '3b82f6' },
          { name: '🟡 Yellow (Warning)', value: 'eab308' },
          { name: '🟠 Orange (Highlight)', value: 'f97316' },
          { name: '⚪ White', value: 'ffffff' },
          { name: '⚫ Black', value: '1f2937' },
          { name: '🩷 Pink', value: 'ec4899' },
          { name: '🩵 Cyan', value: '06b6d4' }
        )
    ),

  async execute(interaction, supabase, client) {
    const channel = interaction.options.getChannel('channel');
    const color = interaction.options.getString('color') || '7c3aed';

    const modal = new ModalBuilder()
      .setCustomId(`embed_modal_${channel.id}_${color}`)
      .setTitle('Create Custom Embed');

    const titleInput = new TextInputBuilder()
      .setCustomId('embed_title')
      .setLabel('Title')
      .setStyle(TextInputStyle.Short)
      .setPlaceholder('Enter embed title')
      .setMaxLength(256)
      .setRequired(true);

    const descriptionInput = new TextInputBuilder()
      .setCustomId('embed_description')
      .setLabel('Description')
      .setStyle(TextInputStyle.Paragraph)
      .setPlaceholder('Enter embed description (supports markdown)')
      .setMaxLength(4000)
      .setRequired(true);

    const imageInput = new TextInputBuilder()
      .setCustomId('embed_image')
      .setLabel('Image URL (optional)')
      .setStyle(TextInputStyle.Short)
      .setPlaceholder('https://example.com/image.png')
      .setRequired(false);

    const thumbnailInput = new TextInputBuilder()
      .setCustomId('embed_thumbnail')
      .setLabel('Thumbnail URL (optional)')
      .setStyle(TextInputStyle.Short)
      .setPlaceholder('https://example.com/thumbnail.png')
      .setRequired(false);

    const footerInput = new TextInputBuilder()
      .setCustomId('embed_footer')
      .setLabel('Footer text (optional)')
      .setStyle(TextInputStyle.Short)
      .setPlaceholder('Enter footer text')
      .setMaxLength(2048)
      .setRequired(false);

    const row1 = new ActionRowBuilder().addComponents(titleInput);
    const row2 = new ActionRowBuilder().addComponents(descriptionInput);
    const row3 = new ActionRowBuilder().addComponents(imageInput);
    const row4 = new ActionRowBuilder().addComponents(thumbnailInput);
    const row5 = new ActionRowBuilder().addComponents(footerInput);

    modal.addComponents(row1, row2, row3, row4, row5);

    await interaction.showModal(modal);
  },
};
