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

module.exports = {
  data: new SlashCommandBuilder()
    .setName('verify-status')
    .setDescription('Check your account verification status'),

  async execute(interaction, supabase, client) {
    if (!supabase) {
      return interaction.reply({ 
        content: 'This feature requires database configuration.', 
        ephemeral: true 
      });
    }
    
    await interaction.deferReply({ ephemeral: true });

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

      const { data: pending } = await supabase
        .from('discord_verifications')
        .select('verification_code, expires_at')
        .eq('discord_id', interaction.user.id)
        .maybeSingle();

      if (link) {
        const linkedDate = new Date(link.linked_at).toLocaleDateString('en-US', {
          year: 'numeric',
          month: 'long',
          day: 'numeric'
        });

        const embed = new EmbedBuilder()
          .setColor(0x00ff00)
          .setTitle('✅ Verified Account')
          .setDescription('Your Discord account is linked to AeThex.')
          .addFields(
            { name: '🔗 AeThex User ID', value: `\`${link.user_id}\``, inline: true },
            { name: '📅 Linked Since', value: linkedDate, inline: true }
          )
          .setFooter({ text: 'Use /unlink to remove this connection' });

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

      if (pending) {
        const expiresAt = new Date(pending.expires_at);
        const now = new Date();
        const expired = expiresAt < now;

        if (expired) {
          const embed = new EmbedBuilder()
            .setColor(0xff9500)
            .setTitle('⏰ Verification Expired')
            .setDescription('Your previous verification code has expired.')
            .addFields(
              { name: '💡 Next Step', value: 'Run `/verify` to generate a new code.' }
            );

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

        const remainingMs = expiresAt - now;
        const remainingMins = Math.ceil(remainingMs / 60000);

        const embed = new EmbedBuilder()
          .setColor(0x7289da)
          .setTitle('⏳ Verification Pending')
          .setDescription('You have a pending verification code.')
          .addFields(
            { name: '📝 Code', value: `\`${pending.verification_code}\``, inline: true },
            { name: '⏱️ Expires In', value: `${remainingMins} minute${remainingMins !== 1 ? 's' : ''}`, inline: true },
            { name: '💡 Next Step', value: 'Visit [aethex.dev/discord-verify](https://aethex.dev/discord-verify) to complete verification.' }
          );

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

      const embed = new EmbedBuilder()
        .setColor(0xff5555)
        .setTitle('❌ Not Verified')
        .setDescription('Your Discord account is not linked to AeThex.')
        .addFields(
          { name: '💡 Get Started', value: 'Run `/verify` to link your account and unlock exclusive features!' }
        );

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

    } catch (error) {
      console.error('Verify status error:', error);
      
      const embed = new EmbedBuilder()
        .setColor(0xff0000)
        .setTitle('❌ Error')
        .setDescription('Failed to check verification status. Please try again.');

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