const moment = require("moment/moment");
const zulip = require("zulip-js");
const { exec } = require("child_process");

const sendNotification = async () => {
  try {
    const config = {
      username: "networktool-bot@zulip.ipsupply.com.au",
      apiKey: "0jMAmOuhfLvBqKJikv5oAkyNM4RIEoAM",
      realm: "https://zulip.ipsupply.com.au",
    };
    const client = await zulip(config);
    let auto1 = "";
    let auto2 = "";
    let log = "";

    exec("nc -zv 172.16.7.13 8002", (error, stdout, stderr) => {
      if (error) {
        //   console.error(`Error executing command: ${error.message}`);
        return;
      }

      if (stdout !== "") {
        auto1 += stdout;
      } else {
        auto1 += stderr;
      }
    });

    exec("nc -zv 172.16.7.13 5000", (error, stdout, stderr) => {
      if (error) {
        //   console.error(`Error executing command: ${error.message}`);
        return;
      }

      if (stdout !== "") {
        auto2 += stdout;
      } else {
        auto2 += stderr;
      }
    });

    exec("nc -zv 172.16.6.23 7777", (error, stdout, stderr) => {
      if (error) {
        exec("/root/.nvm/versions/node/v16.14.0/bin/pm2 restart all", (error, stdout, stderr) => {
          if (error) {
            console.log("RESTART: ", stdout);
            let params = {
              type: "stream",
              to: "networkToolBot",
              topic: "Log service",
              content: "Can't restart server. Please check!\n```\n"+error+"\n```",
            };

            client.messages.send(params);
          }
          //   console.log("RESTART: ", stdout);
          let params = {
            type: "stream",
            to: "networkToolBot",
            topic: "Log service",
            content: "Restarting ...",
          };

          client.messages.send(params);
        });
        return;
      }
      if (stdout !== "") {
        log += stdout;
      } else {
        log += stderr;
      }
    });

    setTimeout(async () => {
      console.log({ auto1: auto1, auto2: auto2, log: log });

      if (process.argv[2] === "daily") {
        let autoStatus =
          auto1?.includes("succeeded") && auto2?.includes("succeeded")
            ? ":check:"
            : ":red_circle:";
        let logStatus = log?.includes("succeeded") ? ":check:" : ":red_circle:";

        let time = moment(Date.now()).format("HH:mm - DD/MM");
        let content =
          "|  |Server| System time| Status \n|---|:---:|:---|:---:\n|1|auto.danielvu.com|**" +
          time +
          "**|" +
          autoStatus +
          "|\n|2|logs.danielvu.com|**" +
          time +
          "**|" +
          logStatus +
          "|";
        let params = {
          type: "stream",
          to: "System Logs",
          topic: "stream events",
          content: content,
        };

        // let params = {
        //   type: "stream",
        //   to: "Result test - auto.nswteam.net",
        //   topic: "AU-dev",
        //   content: content,
        // };

        await client.messages.send(params);
      }
    }, 5000);
  } catch (error) {
    console.log(error);
  }
};

sendNotification();
