import { db } from "@link-stack/bridge-common";
import * as signalApi from "@link-stack/signal-api";
const { Configuration, MessagesApi } = signalApi;

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

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

  const { phoneNumber: number } = bot;
  const config = new Configuration({
    basePath: process.env.BRIDGE_SIGNAL_URL,
  });
  const messagesClient = new MessagesApi(config);
  const response = await messagesClient.v2SendPost({
    data: {
      number,
      recipients: [recipient],
      message,
    },
  });

  console.log({ response });
};

export default sendSignalMessageTask;
