const axios = require("axios");
const config = require("../config");
const { getAccessToken } = require("./token");

async function createSubscription(sessionId) {
  try {
    const token = getAccessToken();

    await axios.post(
      "https://api.twitch.tv/helix/eventsub/subscriptions",
      {
        type: "channel.channel_points_custom_reward_redemption.add",
        version: "1",
        condition: {
          broadcaster_user_id: config.broadcasterId,
        },
        transport: {
          method: "websocket",
          session_id: sessionId,
        },
      },
      {
        headers: {
          "Client-ID": config.clientId,
          Authorization: `Bearer ${token}`,
        },
      }
    );

    console.log("📡 Subscription created");
  } catch (e) {
    console.error("❌ Subscription error:", e.response?.data || e.message);
  }
}

module.exports = { createSubscription };