// pages/api/cpl/messages.ts
import { NextApiRequest, NextApiResponse } from "next";
import messagesRaw from "@/mocks/device-cgi-simulator/meldungen/messages_all.json";

export default function handler(req: NextApiRequest, res: NextApiResponse) {
  const { fromDate, toDate } = req.query;

  if (!fromDate || !toDate) {
    return res
      .status(400)
      .json({ error: "fromDate und toDate sind erforderlich" });
  }

  const from = new Date(String(fromDate));
  const to = new Date(String(toDate));
  to.setHours(23, 59, 59, 999); // ganzen Tag einschließen

  const filtered = messagesRaw.filter((m) => {
    const t = new Date(m.t);
    return t >= from && t <= to;
  });

  res.status(200).json(filtered);
}
