// inventoryController.js
import buildAddFixedPriceItemRequestXML from "../utils/buildAddFixedPriceItemRequestXML.js";
import fetch from "node-fetch";
import fetchEbayUserToken from "../utils/fetchEbayUserToken.js"; // Adjust the import path according to your project structure

export const addItem = async (req, res) => {
  const itemDetails = req.body; // Assuming item details are sent in the request body
  itemDetails.userToken =
    "v^1.1#i^1#r^0#I^3#f^0#p^3#t^H4sIAAAAAAAAAOVZf2wbVx2P86Nd1HWAViWsLZrntQMtPd+7O9vnO2ozJ3bidPnhxnaXBKHo3d075zX3q/fu4ngTKI22wIS6CaqhijIUxoSEJjQ2YCCtUHVD2h/AtE6CakKiILSu4w8KRQwmYOLO+VE3U3/EDqol/M/53n1/fb4/770D81s671/MLv5je2Br69I8mG8NBJhtoHNLR88dba07O1pADUFgaX7PfPtC28X9BOqaJY4hYpkGQcE5XTOIWF1MhFzbEE1IMBENqCMiOrKYTw0PiWwYiJZtOqZsaqHgYDoR4gQ1zsQERQE8w3AxwVs1VmUWzEQIMjwAQkyNCHxc5TjgPSfERYMGcaDhJEIsYCMU4CiWKTCCGAUiAGGBFyZDwUPIJtg0PJIwCCWr5opVXrvG1uubCglBtuMJCSUHU/350dRgOjNS2E/XyEqu+CHvQMclV9/1mQoKHoKai66vhlSpxbwry4iQEJ1c1nC1UDG1akwd5lddzQtcjFeRzHMgGpPjaFNc2W/aOnSub4e/ghVKrZKKyHCwU7mRRz1vSIeR7KzcjXgiBtNB/3LQhRpWMbIToUxvaqKYz4yFgvlczjZnsYIUHykbY0AsxvM8F0paNtaRho+4WIG+L1RsKITjVlQuy11x+DqdfaahYJ+FBEdMpxd59qP1XorUeMkjGjVG7ZTq+LbV0LHMmjejk354l+PpOtOGH2Gkey4JVm9vHIvV5LiSDpuVHiyrCKogKJIKUBww/Ep6+LXeUIok/SilcjnatwVJsELp0J5BjqVBGVGy515XRzZWRC6qslxcRZQSE1QqIqgqJUWVGMWoCAGEJEkW4v+fmeI4NpZcB61ly/oHVbiJUF42LZQzNSxXQutJqn1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GJBbLfe2XL5maeyfTszo1+7/5FC5Y2Tr7XcXvO1culz4ONr3ys725htNR8vwe4rTzqYj3RvZyOAYxlvtwUAmAT3XnnaznS170DfPVe66y8zj59/7zL9aHLg6HPHXimC7WtEgUBHS/tCoGXhAv3uyO/Gz8gf7DjPn7rjm7suJWInTle2HrvISn8MHbv75B9gl/DkF9+57e7vLQ088dzx9NP2qy+h0z+U9m6d6/70j8+d/cF7u5a+3fc6aQlkfv+VF8Lf+flPs/R/7jox0bP13d17U0v3DL5zZvGjv33xr2/96/BTn1/s7tzdsfTsSBeZ/0Tuto4dU5k9f+rdta+r+Frq/e7Pvn36+Y6Xf/Po+08kzh4dv/TMBz/ZfuLJb2Tf1sao7IL6z5dePv6jT77xq19MHy/NPuYKr3+r/yz/sS/8+q39X7Ze7DrX/7evX3h2T/bvb54ipx54IT+9T7tA/1u5RN/32Kcun7l47s/7WvJvSo+3fX9x29NffeAzrx7ofOT8l86c3Ne9HMv/AqyXZcRHHgAA";
  console.log(itemDetails);
  const xmlRequest = buildAddFixedPriceItemRequestXML(itemDetails);

  try {
    const response = await fetch("https://api.ebay.com/ws/api.dll", {
      method: "POST",
      headers: {
        "Content-Type": "text/xml",
        "X-EBAY-API-SITEID": "0",
        "X-EBAY-API-CALL-NAME": "AddFixedPriceItem",
        "X-EBAY-API-COMPATIBILITY-LEVEL": "967", // Ensure this is the current compatibility level
        "X-EBAY-API-APP-NAME": process.env.EBAY_CLIENT_ID,
        "X-EBAY-API-DEV-NAME": process.env.EBAY_DEV_ID,
        "X-EBAY-API-CERT-NAME": process.env.EBAY_CLIENT_SECRET,
      },
      body: xmlRequest,
    });

    if (!response.ok) {
      throw new Error(`eBay API responded with status ${response.status}`);
    }

    const responseData = await response.text();
    // Parse the XML response and handle it appropriately
    // You might need an XML parser here to handle the response

    res.json({ success: true, data: responseData }); // Send back a success response
  } catch (error) {
    console.error("Error adding item to eBay:", error);
    res.status(500).json({
      success: false,
      message: "Failed to add item to eBay",
      error: error.message,
    });
  }
};
