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

interface ReceiveSignalMessageTaskOptions {
  token: string;
  to: string;
  from: string;
  messageId: string;
  sentAt: string;
  message: string;
  attachment?: string;
  filename?: string;
  mimeType?: string;
}

const receiveSignalMessageTask = async ({
  token,
  to,
  from,
  messageId,
  sentAt,
  message,
  attachment,
  filename,
  mimeType,
}: ReceiveSignalMessageTaskOptions): Promise<void> => {
  console.log({ token, to, from });
  const worker = await getWorkerUtils();
  const row = await db
    .selectFrom("SignalBot")
    .selectAll()
    .where("id", "=", token)
    .executeTakeFirstOrThrow();

  const backendId = row.id;
  const payload = {
    to,
    from,
    message_id: messageId,
    sent_at: sentAt,
    message,
    attachment,
    filename,
    mime_type: mimeType,
  };

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

export default receiveSignalMessageTask;
