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

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

  const filePath = path.join(
    process.cwd(),
    "apiMockData",
    "SERVICE",
    "analogeEingaengeMockData.js"
  );

  try {
    const { updates } = req.body;

    if (!Array.isArray(updates)) {
      return res.status(400).json({ error: "Ungültige Datenstruktur" });
    }

    const raw = await fs.readFile(filePath, "utf-8");

    // Teile den Inhalt in:
    // 1. Vor dem ersten var
    // 2. Die var-Zuweisungen (Arrays)
    // 3. Nach dem letzten var (z. B. Block-Kommentare)

    const varRegex = /var\s+(\w+)\s*=\s*\[([\s\S]*?)\];/g;
    let match;

    const updateMap: Record<string, string[]> = {};
    const variableLines: string[] = [];
    let matchStartIndexes: number[] = [];

    while ((match = varRegex.exec(raw)) !== null) {
      const [fullMatch, key, valuesBlock] = match;
      const values = valuesBlock
        .split(",")
        .map((v) => v.trim())
        .filter((v) => v.length > 0);

      updateMap[key] = values;
      variableLines.push(fullMatch);
      matchStartIndexes.push(match.index);
    }

    // Kommentare vor und nach den Variablen extrahieren
    const firstVarIndex = matchStartIndexes[0] ?? 0;
    const lastVarMatch = variableLines[variableLines.length - 1] ?? "";
    const lastVarIndex = raw.lastIndexOf(lastVarMatch);
    const commentsBefore = raw.slice(0, firstVarIndex).trim();
    const commentsAfter = raw.slice(lastVarIndex + lastVarMatch.length).trim();

    // Updates anwenden
    for (const { key, index, value } of updates) {
      if (!updateMap[key]) {
        console.warn(`⚠️ Schlüssel '${key}' fehlt – wird übersprungen`);
        continue;
      }
      updateMap[key][index] =
        typeof value === "string" ? `"${value}"` : value.toString();
    }

    // Arrays immer einzeilig schreiben
    const variablesBlock = Object.entries(updateMap)
      .map(([key, values]) => `var ${key} = [${values.join(", ")}];`)
      .join("\n");

    const finalContent =
      [commentsBefore, variablesBlock, commentsAfter]
        .filter(Boolean)
        .join("\n\n") + "\n";

    await fs.writeFile(filePath, finalContent, "utf-8");

    res.status(200).json({ message: "Mockdaten gespeichert." });
  } catch (err) {
    console.error("❌ Fehler beim Schreiben:", err);
    res.status(500).json({ error: "Interner Serverfehler" });
  }
}
