const express = require("express");
const fs = require("fs");
const path = require("path");
const cors = require("cors");
const cookieParser = require("cookie-parser");

const app = express();
const PORT = 3000;

app.use(cors({ origin: "https://schiri.marc-wieland.de", credentials: true }));
app.use(cookieParser());
app.use(express.json());


app.get("/api/questions/:mode", (req, res) => {
  const mode = req.params.mode?.toLowerCase();
  if (!["d", "c"].includes(mode)) {
    return res.status(400).json({ error: "Ungültiger Modus. Nur 'd' oder 'c' erlaubt." });
  }

  const filePath = path.join(__dirname, `${mode}_condensed.json`);
  try {
    const data = fs.readFileSync(filePath, "utf8");
    res.json(JSON.parse(data));
  } catch (err) {
    res.status(500).json({ error: "Fehler beim Laden der Fragen." });
  }
});

app.listen(PORT, () => {
  console.log(`📡 Backend läuft auf http://localhost:${PORT}`);
});
