const express = require("express");
const app = express();
app.use(express.json());
app.post("/api/turnkey-proxy", async (req, res) => {
  // The original request URL e.g. https://api.turnkey.com/public/v1/submit/create_sub_organization
  const turnkeyApiRequestURL = req.headers["X-Turnkey-Request-Url"];
  // Remove the 'X-Turnkey-Request-Url' header
  delete req.headers["X-Turnkey-Request-Url"];
  try {
    // Forward the request to the original URL
    const response = await fetch(turnkeyApiRequestURL, {
      method: "POST",
      headers: req.headers,
      body: JSON.stringify(req.body),
    });
    // Get the response data
    const data = await response.json();
    // Data will be the response from the create sub-organization request
    // You may save this data to your database
    // Important: Send the exact response back to the client
    // This is necessary for TurnkeySDK to process the response correctly
    res.status(response.status).json(data);
  } catch (error) {
    console.error("Error forwarding request:", error);
    res.status(500).send("Internal Server Error");
  }
});
// Start the server
app.listen(3000, () => {
  console.log("Server is running on port 3000");
});