// /pages/api/cpl/updateTdrReferenceCurveAPIHandler.ts
import { NextApiRequest, NextApiResponse } from "next";
import path from "path";
import { promises as fs } from "fs";

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  if (req.method !== "POST") {
    return res.status(405).json({ error: "Method not allowed" });
  }

  const { slot, data } = req.body;

  if (!slot || !data) {
    return res.status(400).json({ error: "Missing slot or data" });
  }

  const filePath = path.join(
    process.cwd(),
    "apiMockData",
    "tdr-reference-curves",
    `slot${slot}.json`
  );

  try {
    await fs.writeFile(filePath, JSON.stringify(data, null, 2), "utf-8");
    res.status(200).json({ success: true });
  } catch (error) {
    res.status(500).json({ error: "Failed to save file" });
  }
}
