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

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const filePath = path.join(
    process.cwd(),
    "mocks",
    "device-cgi-simulator",
    "SERVICE",
    "last20MessagesMockData.json"
  );

  try {
    const data = await fs.readFile(filePath, "utf-8");

    res.setHeader("Content-Type", "text/javascript"); // wichtig!
    res.status(200).send(data);
  } catch (error) {
    console.error("Fehler beim Laden der letzten 20 Meldungen:", error);
    res.status(500).json({ error: "Interner Serverfehler" });
  }
}
