import { Interaction } from 'discord.js';
import { EventHandler } from '../utils/types';
import { context } from '../config/context';

const event: EventHandler = {
  name: 'interactionCreate',
  async execute(interaction: Interaction) {
    if (interaction.isChatInputCommand()) {
      const handler = context.commandHandler;
      if (!handler) return;
      await handler.handleInteraction(interaction);
      return;
    }

    if (interaction.isButton()) {
      if (interaction.customId.startsWith('event:')) {
        const [_, action, eventId] = interaction.customId.split(':');
        if (action === 'signup' || action === 'signoff') {
          await context.events.handleButton(interaction as any, action as any, eventId);
          return;
        }
      }
      if (interaction.customId.startsWith('register:')) {
        await context.register.handleButton(interaction as any);
        return;
      }
      await context.tickets.handleButton(interaction);
    }

    if (interaction.isModalSubmit()) {
      if (interaction.customId.startsWith('register:submit:')) {
        await context.register.handleModal(interaction as any);
        return;
      }
    }
  }
};

export default event;
