const config = require("../config");

const hydrate = require("../rewards/hydrate");
const sound = require("../rewards/sound");

const seen = new Set();

function handleEvent(payload) {
  const { subscription, event } = payload;

  if (
    subscription.type !==
    "channel.channel_points_custom_reward_redemption.add"
  ) {
    return;
  }

  if (seen.has(event.id)) return;
  seen.add(event.id);
  setTimeout(() => seen.delete(event.id), 60000);

  console.log(`🎁 ${event.user_name} → ${event.reward.title}`);

  switch (event.reward.id) {
    case config.rewards.HYDRATE:
      hydrate(event);
      break;

    case config.rewards.SOUND:
      sound(event);
      break;

    default:
      console.log("Unknown reward:", event.reward.id);
  }
}

module.exports = { handleEvent };