import type { Log } from "../db/schema/logs.js";
import { validateEnv } from "../utils/envValidator.js";

const env = validateEnv(process.env);

export async function sendNotify(log: any) {
    const webhookUrl = process.env.WEBHOOK_URL!;
    let payload = {
        embeds: [
            {
                title: `🚨 ${env.PROD_PLANT_TOKEN}: encounter a critical error `,
                description: `Where was the error: ${log.module}${
                    log.subModule ? `-${log.subModule}` : ""
                }`,
                color: 0xff0000, // red
                fields: [
                    {
                        name: "Message",
                        value: log.message,
                        inline: false,
                    },
                    {
                        name: "Hostname",
                        value: log.hostname,
                        inline: false,
                    },
                    {
                        name: "Stack",
                        value: "```" + (log.stack ?? "no stack") + "```",
                    },
                ],
                footer: {
                    text: "LST Logger 💀",
                },
                timestamp: new Date().toISOString(),
            },
        ],
    };

    await fetch(webhookUrl, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(payload),
    });
}
