const path = require("path");
require("dotenv").config({
  path: path.resolve(
    process.cwd(),
    `.env.${process.env.NODE_ENV || "development"}`
  ),
});

const client = require("../graphql-client/graphql-client").client;
const queries = require("../graphql-client/queries");
const { phone } = require("phone");
const logger = require("../utils/logger");
const { admin } = require("../firebase/firebase-handler");

exports.status = (req, res) => {
  const { SmsSid, SmsStatus } = req.body;
  client
    .request(queries.UPDATE_MESSAGE_STATUS, {
      msid: SmsSid,
      fields: { status: SmsStatus },
    })
    .then((response) => {
      logger.log("sms-status-update", "DEBUG", "api", null, {
        msid: SmsSid,
        fields: { status: SmsStatus },
      });
    })
    .catch((error) => {
      logger.log("sms-status-update-error", "ERROR", "api", null, {
        msid: SmsSid,
        fields: { status: SmsStatus },
        error,
      });
    });
  res.sendStatus(200);
};

exports.markConversationRead = async (req, res) => {
  const { conversationid, imexshopid } = req.body;
  admin.messaging().send({
    topic: `${imexshopid}-messaging`,
    // notification: {
    //   title: `ImEX Online Message - ${data.phone_num}`,
    //   body: message.image_path ? `Image ${message.text}` : message.text,
    //   imageUrl: "https://thinkimex.com/img/logo512.png",
    // },
    data: {
      type: "messaging-mark-conversation-read",
      conversationid: conversationid || "",
    },
  });
  res.send(200);
};

// Inbound Sample
// {
//     "SmsSid": "SM5205ea340e06437799d9345e7283457c",
//     "SmsStatus": "queued",
//     "MessageStatus": "queued",
//     "To": "+16049992002",
//     "MessagingServiceSid": "MG6e259e2add04ffa0d0aa355038670ee1",
//     "MessageSid": "SM5205ea340e06437799d9345e7283457c",
//     "AccountSid": "AC6c09d337d6b9c68ab6488c2052bd457c",
//     "From": "+16043301606",
//     "ApiVersion": "2010-04-01"
//   }
