import { getMealInfo } from "./meal";

const WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL as string;

export async function Discord(MLSV_YMD: string) {
  const mealInfo = await getMealInfo(MLSV_YMD);
  //const isVTS = vts.VTS임(MLSV_YMD);

  const data = {
    // To include VTS info, uncomment and define isVTS:
    // content: `${MLSV_YMD.slice(0, 4)}년 ${MLSV_YMD.slice(4, 6)}월 ${MLSV_YMD.slice(6, 8)}일 급식 정보${isVTS ? " | with V.T.S." : ""}`,
    content: `${MLSV_YMD.slice(0, 4)}년 ${MLSV_YMD.slice(
      4,
      6
    )}월 ${MLSV_YMD.slice(6, 8)}일 급식 정보`,
    username: "@today.isangjeong",
    embeds: [
      {
        title: "🏫 | 인천상정중학교",
        description: `🔥 ${mealInfo.kcal}\n\n${mealInfo.meal}`,
      },
    ],
  };

  console.log("🏓 | Sending Payload");
  const response = await fetch(WEBHOOK_URL, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(data),
  });

  if (!response.ok) {
    console.error("Error sending Discord webhook:", response.statusText);
  } else {
    console.log(`✨ | Payload successfully, code ${response.status}`);
  }
}
