import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.resolve(__dirname, '..');
const DIST = path.join(ROOT, 'dist');

const posts = JSON.parse(fs.readFileSync(path.join(ROOT, 'src/content/posts.json'), 'utf8'));
const pages = JSON.parse(fs.readFileSync(path.join(ROOT, 'src/content/pages.json'), 'utf8'));

const cats = new Set();
for (const p of posts) (p.categorySlugs || []).forEach((s) => cats.add(s));

const routes = ['/', '/news/'];
for (const p of posts) routes.push(`/${p.slug}/`);
for (const p of pages) routes.push(`/${p.slug}/`);
for (const c of cats) routes.push(`/cat/${c}/`);

fs.mkdirSync(DIST, { recursive: true });
fs.writeFileSync(path.join(DIST, 'routes.json'), JSON.stringify(routes, null, 2));
console.log(`routes: ${routes.length} → dist/routes.json`);
