import { Events, Interaction } from "discord.js";
import { BotClient } from "../types/botClient";
import logger from "../utils/logger";

export default {
  name: Events.InteractionCreate,
  async execute(interaction: Interaction, client?: BotClient) {
    if (!interaction.isChatInputCommand()) return;

    if (!client) {
      logger.error("Client not provided to interaction handler");
      return;
    }

    const command = client.commands.get(interaction.commandName);

    if (!command) {
      await interaction.reply({
        content: "Command not found!",
        ephemeral: true,
      });
      return;
    }

    try {
      await command.execute(interaction, client);
    } catch (error) {
      logger.error(`Error executing command ${interaction.commandName}:`, error);
      if (interaction.isRepliable()) {
        await interaction.reply({
          content: "There was an error while executing this command!",
          ephemeral: true,
        });
      }
    }
  },
};
