import { db } from "@link-stack/bridge-common";

interface SendWhatsappMessageTaskOptions {
  token: string;
  recipient: string;
  message: any;
}

const sendWhatsappMessageTask = async ({
  message,
  recipient,
  token,
}: SendWhatsappMessageTaskOptions): Promise<void> => {
  const bot = await db
    .selectFrom("WhatsappBot")
    .selectAll()
    .where("token", "=", token)
    .executeTakeFirstOrThrow();

  const url = `${process.env.BRIDGE_WHATSAPP_URL}/api/bots/${bot.id}/send`;
  const params = { message, phoneNumber: recipient };
  console.log({ params });
  const result = await fetch(url, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(params),
  });

  console.log({ result });
};

export default sendWhatsappMessageTask;
