import type { BotContext } from '../../types.js';
import { logger } from '../../logger.js';
import { processStarboardReaction } from './service.js';

export function registerStarboardEvents(context: BotContext): void {
  const handleReaction = async (
    reaction: Parameters<typeof processStarboardReaction>[1]
  ): Promise<void> => {
    try {
      await processStarboardReaction(context, reaction);
    } catch (error) {
      logger.warn({ error }, 'Starboard reaction handler failed');
    }
  };

  context.client.on('messageReactionAdd', async (_reaction, user) => {
    if (user.bot) {
      return;
    }
    await handleReaction(_reaction);
  });

  context.client.on('messageReactionRemove', async (_reaction, user) => {
    if (user.bot) {
      return;
    }
    await handleReaction(_reaction);
  });
}
