/**
 * slides/section4-openswe/compile.js
 * ----------------------------------------------------------------------------
 * Compiles all Open SWE section slides into section4.pptx.
 *
 * Usage:
 *   node compile.js
 *
 * Output:
 *   ../section4.pptx           (relative to this file)
 *   ../section4.pdf            (via libreoffice)
 *   ../section4-page-N.png     (preview images via pdftoppm)
 */
'use strict';

const path = require('path');
const fs = require('fs');

const pptxgen = require('pptxgenjs');
const ds = require('../../design-system');

const { theme, helpers } = ds;

// Import all slide builders in order.
const slides = [
  require('./01-cover'),
  require('./02-what-is-openswe'),
  require('./03-architecture-overview'),
  require('./04-create-deep-agent'),
  require('./05-agents-md-context'),
  require('./06-middleware'),
  require('./07-sandbox-providers'),
  require('./08-sandbox-imports'),
  require('./09-installation-1'),
  require('./10-installation-2'),
  require('./11-github-app'),
  require('./12-langsmith'),
  require('./13-triggers-overview'),
  require('./14-triggers-thread-id'),
  require('./15-webhook-endpoints'),
  require('./16-dashboard-ui'),
  require('./17-per-user-settings'),
  require('./18-customization-6-points'),
  require('./19-three-graphs'),
  require('./20-observability'),
  require('./21-production-ready'),
  require('./22-typescript'),
  require('./23-pros-cons'),
  require('./24-roadmap'),
];

async function main() {
  console.log('[compile] building section4.pptx with ' + slides.length + ' slides');

  const pres = new pptxgen();
  pres.layout = 'LAYOUT_16x9';
  pres.title = 'lc-evo-deck / Stage 4: Open SWE';
  pres.subject = 'LangChain Evolution Deck -- Open SWE section';
  pres.company = 'lc-evo-deck';

  for (let i = 0; i < slides.length; i++) {
    const mod = slides[i];
    const builder = pickBuilder(mod);
    if (typeof builder !== 'function') {
      throw new Error('slide ' + (i + 1) + ': no builder function');
    }
    builder(pres, theme);
  }

  // Output: one level up from this file, into sec4-openswe/ root.
  const outDir = path.resolve(__dirname, '..', '..');
  const outPath = path.join(outDir, 'section4.pptx');
  await pres.writeFile({ fileName: outPath });
  console.log('[compile] wrote ' + outPath);

  return outPath;
}

function pickBuilder(mod) {
  // Each slide file exports either buildX or default.
  if (typeof mod.buildCover === 'function') return mod.buildCover;
  if (typeof mod.buildWhatIsOpenSWE === 'function') return mod.buildWhatIsOpenSWE;
  if (typeof mod.buildArchitectureOverview === 'function') return mod.buildArchitectureOverview;
  if (typeof mod.buildCreateDeepAgent === 'function') return mod.buildCreateDeepAgent;
  if (typeof mod.buildAgentsMdConvention === 'function') return mod.buildAgentsMdConvention;
  if (typeof mod.buildMiddleware === 'function') return mod.buildMiddleware;
  if (typeof mod.buildSandboxProviders === 'function') return mod.buildSandboxProviders;
  if (typeof mod.buildSandboxImports === 'function') return mod.buildSandboxImports;
  if (typeof mod.buildInstallPart1 === 'function') return mod.buildInstallPart1;
  if (typeof mod.buildInstallPart2 === 'function') return mod.buildInstallPart2;
  if (typeof mod.buildGithubApp === 'function') return mod.buildGithubApp;
  if (typeof mod.buildLangSmithSetup === 'function') return mod.buildLangSmithSetup;
  if (typeof mod.buildTriggersOverview === 'function') return mod.buildTriggersOverview;
  if (typeof mod.buildTriggerRouting === 'function') return mod.buildTriggerRouting;
  if (typeof mod.buildWebhookEndpoints === 'function') return mod.buildWebhookEndpoints;
  if (typeof mod.buildDashboardUI === 'function') return mod.buildDashboardUI;
  if (typeof mod.buildPerUserSettings === 'function') return mod.buildPerUserSettings;
  if (typeof mod.buildCustomization6 === 'function') return mod.buildCustomization6;
  if (typeof mod.buildThreeGraphs === 'function') return mod.buildThreeGraphs;
  if (typeof mod.buildObservability === 'function') return mod.buildObservability;
  if (typeof mod.buildProductionReady === 'function') return mod.buildProductionReady;
  if (typeof mod.buildTypescript === 'function') return mod.buildTypescript;
  if (typeof mod.buildProsConsComparison === 'function') return mod.buildProsConsComparison;
  if (typeof mod.buildRoadmap === 'function') return mod.buildRoadmap;
  return null;
}

main().catch(function (err) {
  console.error('[compile] failed:', err);
  process.exit(1);
});
