import axios from "axios";
import querystring from "querystring";
import { createLog } from "../../../../logger/logger.js";
import { saveXml } from "../../../utils/saveXml.js";
//tiCreds
const userid = process.env.USERID || "";
const password = process.env.PASSWORD || "";

export const postToTi = async (data: string, fileName: string) => {
    // 🔹 Save the XML before posting
    saveXml(data, fileName);
    const formBody = querystring.stringify({
        userid,
        password,
        request: data,
    });
    axios
        .post(
            "https://t-insightws.mercurygate.net/MercuryGate/common/remoteService.jsp",
            formBody,
            {
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded",
                },
            }
        )
        .then((response) => {
            //console.log(response.data)
            createLog("info", "ti", "notify", "Data was sent over to TI");
            return {
                success: true,
                message: "Data was sent over to TI",
            };
        })
        .catch((error) => {
            createLog("error", "ti", "notify", error);
            return {
                success: false,
                message: "Error sending data to TI",
                data: error,
            };
        });
};
