/**
 * routes/changelog.js — public release notes at GET /changelog.
 *
 * Owns: rendering docs/changelog-public.md as a public page.
 * Does NOT own: docs/CHANGELOG.md, the engineering record, which is deliberately
 *               NOT published — it names internal hosts, lab environments, ticket
 *               IDs and connection numbers, all banned from public pages by the
 *               architecture check. Rendering it raw would leak exactly what
 *               status-installer.html was cleaned of on 2026-08-06.
 *
 * Two separate files is the point, not an oversight. The engineering log stays
 * blunt and detailed because that is what makes it useful; the public log is
 * curated per milestone rather than per commit, so it does not double the work the
 * CLAUDE.md bundling rule already requires.
 *
 * Read from disk per request (the file is small and this page is low-traffic), so
 * a correction ships with the next deploy and needs no cache bust.
 */

'use strict';

const express = require('express');
const fs = require('fs');
const path = require('path');
const { markdownToHtml } = require('./blog');

const router = express.Router();
const SRC = path.join(__dirname, '..', 'docs', 'changelog-public.md');

function page(bodyHtml) {
  return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Release Notes — TuneVault</title>
<meta name="description" content="What shipped in TuneVault: AutoPilot self-healing, MCP tools for Claude, agent configuration editing, Oracle licensing controls, EBS operations and installer fixes."/>
<meta property="og:title" content="TuneVault Release Notes"/>
<meta property="og:description" content="What shipped in TuneVault, month by month."/>
<meta property="og:type" content="website"/>
<meta property="og:url" content="https://tunevault.app/changelog"/>
<link rel="canonical" href="https://tunevault.app/changelog"/>
<link rel="preconnect" href="https://fonts.googleapis.com"/>
<link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;600;700&family=JetBrains+Mono:wght@400;600&display=swap" rel="stylesheet"/>
<style>
  *,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
  :root{--bg:#0a0a0a;--surface:#111;--border:#1e1e1e;--text:#e8e8e8;--text-dim:#8a8a92;--accent:#f0a830;--code-bg:#161616}
  body{background:var(--bg);color:var(--text);font-family:'Space Grotesk',-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;line-height:1.7;min-height:100vh}
  main{max-width:760px;margin:0 auto;padding:56px 24px 96px}
  .page-kicker{font-size:12px;letter-spacing:.08em;text-transform:uppercase;color:var(--accent);margin-bottom:10px}
  .md h1{font-size:2.1rem;font-weight:700;line-height:1.25;margin-bottom:14px}
  .md h2{font-size:1.35rem;font-weight:700;margin:48px 0 6px;padding-bottom:8px;border-bottom:1px solid var(--border)}
  .md h3{font-size:1.05rem;font-weight:700;margin:28px 0 8px;color:var(--accent)}
  .md p{margin-bottom:14px;color:var(--text)}
  .md ul,.md ol{margin:14px 0 14px 22px}
  .md li{margin-bottom:6px}
  .md a{color:var(--accent)}
  .md hr{border:none;border-top:1px solid var(--border);margin:40px 0}
  .md code{background:var(--code-bg);border:1px solid var(--border);border-radius:4px;padding:1px 5px;font-family:'JetBrains Mono',monospace;font-size:.86em;overflow-wrap:anywhere}
  .md pre{background:var(--code-bg);border:1px solid var(--border);border-radius:8px;padding:14px 16px;overflow-x:auto;margin:14px 0}
  .md pre code{background:none;border:none;padding:0}
  .md strong{color:#fff}
  .md em{color:var(--text-dim)}
  .backlink{display:inline-block;margin-bottom:28px;color:var(--text-dim);text-decoration:none;font-size:13px}
  .backlink:hover{color:var(--accent)}
  @media(max-width:600px){main{padding:36px 18px 64px}.md h1{font-size:1.6rem}}
</style>
</head>
<body>
<nav id="nav-root"></nav>
<main>
  <a class="backlink" href="/">&larr; TuneVault</a>
  <div class="page-kicker">Release notes</div>
  <div class="md">${bodyHtml}</div>
</main>
<script src="/nav-component.js"></script>
<script>if (window.tvNav && window.tvNav.init) window.tvNav.init('changelog');</script>
</body>
</html>`;
}

router.get('/', (req, res) => {
  let md;
  try {
    md = fs.readFileSync(SRC, 'utf8');
  } catch (err) {
    console.error('[changelog] cannot read changelog-public.md:', err.message);
    return res.status(500).send('Release notes are temporarily unavailable.');
  }
  // The file opens with an H1 and an internal-facing note about why the
  // engineering log is not published; strip that preamble for visitors.
  md = md.replace(/^#\s+TuneVault release notes[\s\S]*?^---\s*$/m, '').trim();
  res.set('Cache-Control', 'public, max-age=300');
  res.send(page('<h1>Release notes</h1>' + markdownToHtml(md)));
});

module.exports = router;
