import { whatsappLog } from "../../serviceLog";
import CONSTANTS from "../../../config/constants";
import whatsappOccupancyDB from "../model/occupancy.model";
import moment from "moment";
import whatsappPendingMessagesDB from "../../../models/whatsappPendingMessages.model";
import { connectRedis } from "../../../config/redis/redis";

const addMoveInMoveOutToQueue = async () => {
  try {
    connectRedis();
    let clientId = 5;
    const moveIns = await whatsappOccupancyDB.moveInsByClientIdAndDate({
      clientId: clientId,
      moveInDate: moment().format("YYYY-MM-DD"),
    });
    // const moveOuts = await whatsappOccupancyDB.moveOutsByClientIdAndDate({
    //   clientId: 856,
    //   moveOutDate: moment().format("YYYY-MM-DD"),
    // });
    console.log("Move In: ", moveIns);
    let moveInPayload = ``;
    //let moveOutPayload = ``;

    if (moveIns && moveIns.length > 0) {
      console.log("Sending to client");
      //moveInPayload += `There are ${moveIns.length} new Move-Ins today:\n`;
      // for (let moveIn of moveIns) {
      //   moveInPayload += `${moveIn.tenantName}(${moveIn.propName}_${moveIn.flatFloor}_${moveIn.roomNum}),`
      // }
      moveInPayload = moveIns
      .map(
        (moveIn: any) =>
          `${moveIn.tenantName} - ${moveIn.tenantMobile} (${moveIn.propName}_${moveIn.flatFloor}_${moveIn.roomNum})`
      )
    .join(", ");

      let bodyVal = [moment().format('DD-MM-YYYY'), moveInPayload, moveIns.length];
      await whatsappPendingMessagesDB.add({
        clientId: clientId,
        propId: 0,
        userType: CONSTANTS.USER_TYPE.CLIENT,
        userId: clientId,
        userName: "Pallav",
        userMobile: 9599054423,
        templateType: CONSTANTS.WHATSAPP_TEMPLATE_TYPES.CLIENT.MOVEIN_REPORT,
        payload: JSON.stringify(bodyVal),
        file: null,
      });
    } else {
      console.log("No MoveIn found");
    }

    // if (moveOuts && moveOuts.length > 0) {
    //     moveOutPayload += `There are ${moveOuts.length} new Move-Outs today:\n`;
    //   for (let moveOut of moveOuts) {
    //     moveOutPayload += `${moveOut.tenantName}(${moveOut.propName}_${moveOut.flatFloor}_${moveOut.roomNum}) has moved-out on ${moment(moveOut.moveOutDate).format('DD-MM-YYYY')}.\n`
    // }
    // await whatsappPendingMessagesDB.add({
    //   clientId: 0,
    //   propId: 0,
    //   userType: CONSTANTS.USER_TYPE.CLIENT,
    //   userId: 0,
    //   userName: "Yusuf",
    //   userMobile: 7996992100,
    //   templateType: CONSTANTS.WHATSAPP_TEMPLATE_TYPES.CLIENT.OTP,
    //   payload: JSON.stringify(moveOutPayload),
    //   file: null,
    // });
    //}

    return true;
  } catch (error: any) {
    whatsappLog.info(
      `[sendOTPSMS], Error: ${JSON.stringify(error?.response?.data) || error?.message || error
      }`,
    );

    return false;
  }
};

//addMoveInMoveOutToQueue();

addMoveInMoveOutToQueue()
  .then(() => {
    console.log("Script completed successfully");
  })
  .catch((error) => {
    console.error("Script failed:", error);
    process.exitCode = 1;
  })
  .finally(async () => {
    // Import and close your actual database pool here, if applicable.
    // Example: await pool.end();
     process.exit();
  });
