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

interface ReceiveSignalMessageTaskOptions {
  token: string;
  sender: string;
  message: string;
  messageID: string;
  timestamp: string;
}

const receiveSignalMessageTask = async ({
  token,
  sender,
  message,
  messageID,
  timestamp,
}: ReceiveSignalMessageTaskOptions): Promise<void> => {
  console.log({ token, sender, message, messageID, timestamp });
  const worker = await getWorkerUtils();
  const row = await db
    .selectFrom("SignalBot")
    .selectAll()
    .where("id", "=", token)
    .executeTakeFirstOrThrow();

  console.log(row);
  console.log(message);
  const backendId = row.id;
  const payload = {
    to: row.phoneNumber,
    from: sender,
    sent_at: timestamp,
    message,
    message_id: messageID,
  };

  await worker.addJob("common/notify-webhooks", { backendId, payload });
};

export default receiveSignalMessageTask;
