/**
 * GET /api/changelog — Serves CHANGELOG_PENDING.md as parsed JSON.
 * Each line of the form {"d":"...","t":"...","m":"..."} is returned as an array.
 */
import { Router, Request, Response } from "express";
import { readFileSync } from "fs";
import { join } from "path";

export const changelogRouter = Router();

const CHANGELOG_PATH = join(__dirname, "..", "..", "..", "..", "CHANGELOG_PENDING.md");

changelogRouter.get("/", (_req: Request, res: Response) => {
  try {
    const raw = readFileSync(CHANGELOG_PATH, "utf-8");
    const entries = raw
      .split("\n")
      .filter((line) => line.trim().startsWith("{"))
      .map((line) => {
        try { return JSON.parse(line.trim()); }
        catch { return null; }
      })
      .filter(Boolean);

    res.json({ entries, total: entries.length });
  } catch (err) {
    console.error("Changelog read error:", err);
    res.status(500).json({ error: "Could not read changelog" });
  }
});
