const { loadRewards, getReward } = require("../rewards");

loadRewards();

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

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

  const rewardName = event.reward.title;

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

  const reward = getReward(rewardName);

  if (!reward) {
    console.log("Unknown reward:", rewardName);
    return;
  }

  reward(event);
}

module.exports = { handleEvent };