/**
 * Quick PDF Generator for Markdown Files
 * Uses Puppeteer to generate PDFs directly from markdown files
 */

const puppeteer = require('puppeteer');
const marked = require('marked');
const fs = require('fs').promises;
const path = require('path');

async function generatePdfFromMarkdown(markdownPath, outputPath, title) {
  const markdown = await fs.readFile(markdownPath, 'utf-8');
  const html = marked.parse(markdown);

  const fullHtml = `
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>${title}</title>
  <style>
    body {
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
      font-size: 11pt;
      line-height: 1.6;
      color: #1f2937;
      max-width: 900px;
      margin: 0 auto;
      padding: 2cm;
    }
    h1 { font-size: 2rem; margin-top: 2rem; margin-bottom: 1rem; page-break-after: avoid; }
    h2 { font-size: 1.5rem; margin-top: 1.5rem; margin-bottom: 0.75rem; page-break-after: avoid; }
    h3 { font-size: 1.25rem; margin-top: 1.25rem; margin-bottom: 0.5rem; page-break-after: avoid; }
    p { margin-bottom: 1rem; line-height: 1.75; }
    code { background: #f3f4f6; padding: 0.125rem 0.375rem; border-radius: 0.25rem; font-size: 0.875rem; }
    pre { background: #f9fafb; padding: 1rem; border-radius: 0.375rem; overflow-x: auto; page-break-inside: avoid; }
    pre code { background: transparent; padding: 0; }
    table { width: 100%; border-collapse: collapse; margin-bottom: 1.25rem; page-break-inside: avoid; }
    th, td { border: 1px solid #d1d5db; padding: 0.625rem 0.875rem; text-align: left; }
    th { background: #f3f4f6; font-weight: 600; }
    blockquote { border-left: 4px solid #3b82f6; padding-left: 1rem; margin: 1.25rem 0; background: #f9fafb; padding: 0.875rem; }
  </style>
</head>
<body>
  <h1>${title}</h1>
  ${html}
</body>
</html>
  `;

  const browser = await puppeteer.launch({
    headless: true,
    args: ['--no-sandbox', '--disable-setuid-sandbox']
  });

  const page = await browser.newPage();
  await page.setContent(fullHtml, { waitUntil: 'networkidle0' });

  await page.pdf({
    path: outputPath,
    format: 'A4',
    printBackground: true,
    margin: { top: '2cm', right: '2cm', bottom: '2cm', left: '2cm' }
  });

  await browser.close();
  console.log(`✓ Generated: ${path.basename(outputPath)}`);
}

async function main() {
  const files = [
    {
      input: 'docs/research/pluralistic-values-research-foundations.md',
      output: 'public/downloads/pluralistic-values-research-foundations.pdf',
      title: 'Pluralistic Values: Research Foundations'
    },
    {
      input: 'docs/value-pluralism-faq.md',
      output: 'public/downloads/value-pluralism-faq.pdf',
      title: 'Value Pluralism in Tractatus: FAQ'
    },
    {
      input: 'docs/pluralistic-values-deliberation-plan-v2.md',
      output: 'public/downloads/pluralistic-values-deliberation-plan-v2.pdf',
      title: 'Pluralistic Values Deliberation Enhancement Plan'
    }
  ];

  for (const file of files) {
    await generatePdfFromMarkdown(file.input, file.output, file.title);
  }

  console.log('\n✓ All PDFs generated successfully');
}

main().catch(console.error);
