import { WebhookClient } from "discord.js";
import { summary } from "./api.js";
import { readFileSync } from "fs";

const config = JSON.parse(readFileSync(process.argv[2], "utf-8"));
console.log(config);

const webhook = new WebhookClient({ url: config.webhook });

for (let user of config.users) {
  let result = await summary(user.wktoken);

  let current_reviews = result.data.reviews.filter(review => new Date(review.available_at) < new Date()).map(lesson => lesson.subject_ids).flat();
  let future_reviews = result.data.reviews.map(review => review.subject_ids).flat();
  let future_time = new Date(result.data.reviews.at(-1).available_at);

  if (current_reviews.length > 0) {
    let message = `<@${user.discord}> You have reviews! If you do your wanikani now, there's only **${current_reviews.length}**! <t:${Math.floor(future_time / 1000)}:R> you'll have **${future_reviews.length}** reviews.`;

    await webhook.send({
      content: message,
      threadId: config.threadId,
      username: "WaniKani Reminder",
      avatarURL: "https://pbs.twimg.com/profile_images/1213575248911552512/WP11ESTy_400x400.jpg",
      allowedMentions: {
        parse: user.mentionUser ? ["users"] : [],
      }
    })
  }
}