import { mkdir, writeFile, readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { collectSpecs, collectTools, collectMetrics } from './data-collector.js';
import { wrapPage } from './html-templates.js';
import { renderDashboard } from './dashboard-renderer.js';
import { renderSpecPage, renderSpecIndex } from './spec-renderer.js';
import { renderToolsCatalog } from './tools-renderer.js';
import { renderDiagramsPage } from './diagrams-renderer.js';
import { stripFrontmatter } from '../frontmatter-parser.js';
import { extractSectionBody } from '../spec-format/read-technical-section.js';
export async function generateDocsSite(config) {
    const { projectPath, outputDir, title = 'Planu Documentation' } = config;
    const [specs, metrics] = await Promise.all([
        collectSpecs(projectPath),
        collectMetrics(projectPath),
    ]);
    const tools = collectTools(projectPath);
    const navItems = [
        { path: 'index.html', title: 'Dashboard' },
        { path: 'specs/index.html', title: 'Specs' },
        { path: 'tools.html', title: 'Tools' },
        { path: 'architecture.html', title: 'Architecture' },
    ];
    const pages = [];
    pages.push({
        path: 'index.html',
        title: 'Dashboard',
        html: wrapPage(`${title} — Dashboard`, renderDashboard(specs, metrics), navItems),
    });
    pages.push({
        path: 'specs/index.html',
        title: 'Specs',
        html: wrapPage(`${title} — Specs`, renderSpecIndex(specs), navItems),
    });
    for (const spec of specs) {
        let specContent = '';
        try {
            const raw = await readFile(spec.specPath, 'utf-8');
            specContent = stripFrontmatter(raw);
        }
        catch {
            /* missing */
        }
        const techContent = extractSectionBody(specContent, 'Technical');
        pages.push({
            path: `specs/${spec.slug}.html`,
            title: spec.title,
            html: wrapPage(`${spec.id}: ${spec.title}`, renderSpecPage(spec, specContent, techContent), navItems),
        });
    }
    pages.push({
        path: 'tools.html',
        title: 'Tools',
        html: wrapPage(`${title} — Tools`, renderToolsCatalog(tools), navItems),
    });
    pages.push({
        path: 'architecture.html',
        title: 'Architecture',
        html: wrapPage(`${title} — Architecture`, renderDiagramsPage(), navItems),
    });
    await mkdir(outputDir, { recursive: true });
    await mkdir(join(outputDir, 'specs'), { recursive: true });
    for (const page of pages) {
        await writeFile(join(outputDir, page.path), page.html, 'utf-8');
    }
    return { pagesGenerated: pages.length, outputDir, pages: pages.map((p) => p.path) };
}
//# sourceMappingURL=index.js.map