import { FormData } from "zulip-js/lib/helper";
import axios from "axios";
import fs from "fs";

export const uploadFileToZulip = async (filename) => {
  //   try {
  const ZULIP_API_URL = "https://zulip.ipsupply.com.au/api/v1";
  const ZULIP_API_KEY = "0jMAmOuhfLvBqKJikv5oAkyNM4RIEoAM";
  const BOT_EMAIL = "networktool-bot@zulip.ipsupply.com.au";
  // const ZULIP_API_URL = "https://zulip.ipsupply.com.au/api/v1";
  // const ZULIP_API_KEY = "7XGrpwzFtQyUVDQzdwL3hjdVSbLx55yt";
  // const BOT_EMAIL = "joseph.le@apactech.io";
  try {
    
      
      const fileStream = fs.createReadStream("/home/screenshot/"+filename+".png");
      const formData = new FormData();
      formData.append("file", fileStream);

      const response = await axios.post(
        `${ZULIP_API_URL}/user_uploads`,
        formData,
        {
          headers: {
            "Content-Type": "application/octet-stream",
            Authorization: `Basic ${Buffer.from(
              `${BOT_EMAIL}:${ZULIP_API_KEY}`
            ).toString("base64")}`,
          },
        }
      );

      if (response.status === 200) {
        const uploadedFile = response.data.uri;
        return uploadedFile;
      } else {
        console.error("Failed to upload file:", response.statusText);
        return null;
      }
    
  } catch (error) {
    console.log("Error uploading file:", error.message);
  }
};
