const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { getServerMode, getEmbedColor } = require('../utils/modeHelper');

module.exports = {
  data: new SlashCommandBuilder()
    .setName('qr')
    .setDescription('Generate a QR code')
    .addStringOption(option =>
      option.setName('text')
        .setDescription('The text or URL to encode')
        .setRequired(true)
        .setMaxLength(500)
    )
    .addIntegerOption(option =>
      option.setName('size')
        .setDescription('QR code size (default: 200)')
        .setRequired(false)
        .setMinValue(100)
        .setMaxValue(500)
    ),

  async execute(interaction, supabase, client) {
    const text = interaction.options.getString('text');
    const size = interaction.options.getInteger('size') || 200;
    const mode = await getServerMode(supabase, interaction.guildId);

    const encodedText = encodeURIComponent(text);
    const qrUrl = `https://api.qrserver.com/v1/create-qr-code/?size=${size}x${size}&data=${encodedText}`;

    const embed = new EmbedBuilder()
      .setColor(getEmbedColor(mode))
      .setTitle('📱 QR Code Generated')
      .setDescription(`\`${text.length > 100 ? text.substring(0, 100) + '...' : text}\``)
      .setImage(qrUrl)
      .setFooter({ text: `Size: ${size}x${size}` })
      .setTimestamp();

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