/**
 * slides/03-architecture-overview.js
 * ----------------------------------------------------------------------------
 * Slide 03 -- Architecture overview: layered model
 * Diagram + import surface code.
 */
'use strict';

const { helpers, layouts } = require('../../design-system');

function buildArchitectureOverview(pres, theme) {
  const slide = pres.addSlide();
  helpers.slideBase(slide, pres, theme);
  helpers.addHeader(slide, pres, theme, {
    eyebrow: 'STAGE 4',
    sectionNumber: 4,
    title: 'Архитектура: 7 слоев',
  });

  // Left: architecture diagram
  const diagX = 0.5;
  const diagY = layouts.CONTENT_TOP;
  const diagW = 4.4;
  const diagH = 3.5;

  slide.addShape(pres.ShapeType.roundRect, {
    x: diagX, y: diagY, w: diagW, h: diagH,
    fill: { color: theme.palette.bg.elevated },
    line: { color: theme.palette.border.subtle, width: 1 },
    rectRadius: 0.08,
  });

  // 7 stacked layers
  const layers = [
    ['Triggers',     'Slack / Linear / GitHub / Web UI'],
    ['Validation',   'prompt + middleware (HITL, approval)'],
    ['Orchestration','subagents + middleware'],
    ['Context',      'AGENTS.md из репозитория'],
    ['Tools',        'execute, fetch_url, linear_comment, slack_thread_reply'],
    ['Sandbox',      'Modal / Daytona / Runloop / LangSmith'],
    ['Harness',      'create_deep_agent (Deep Agents)'],
  ];

  const layerH = (diagH - 0.3) / layers.length;
  layers.forEach(function (layer, idx) {
    const y = diagY + 0.15 + idx * layerH;
    slide.addShape(pres.ShapeType.rect, {
      x: diagX + 0.15, y: y, w: diagW - 0.3, h: layerH - 0.05,
      fill: { color: idx === layers.length - 1
        ? theme.palette.accent.primary
        : theme.palette.bg.code },
      line: { color: theme.palette.border.subtle, width: 0.5 },
    });
    slide.addText(layer[0], {
      x: diagX + 0.25, y: y + 0.02, w: diagW - 0.5, h: 0.22,
      fontFace: helpers.withFallback(theme.fonts.ui),
      fontSize: theme.sizes.body,
      color: idx === layers.length - 1
        ? theme.palette.text.inverse
        : theme.palette.text.primary,
      bold: true,
    });
    slide.addText(layer[1], {
      x: diagX + 0.25, y: y + 0.22, w: diagW - 0.5, h: layerH - 0.27,
      fontFace: helpers.withFallback(theme.fonts.code),
      fontSize: theme.sizes.caption,
      color: idx === layers.length - 1
        ? theme.palette.text.inverse
        : theme.palette.text.secondary,
    });
  });

  // Right: import surface
  helpers.addCodeBlock(slide, pres, theme, {
    x: 5.2, y: layouts.CONTENT_TOP, w: 4.3, h: 3.5,
    language: 'python',
    code: [
      'from open_swe.agent import create_agent',
      'from open_swe.middleware import (',
      '    check_message_queue_before_model,',
      '    notify_step_limit_reached,',
      '    open_pr_if_needed,',
      '    ToolErrorMiddleware,',
      ')',
      'from open_swe.sandbox import (',
      '    SandboxBackend,',
      '    ModalBackend, DaytonaBackend,',
      ')',
    ].join('\n'),
    filePath: 'open_swe/__init__.py',
    startLine: 1,
  });

  helpers.addPageNumber(slide, pres, theme, 3);
  helpers.addSourceLine(slide, pres, theme, {
    source: 'research/per-tech/openswe.md: 26-50',
  });
  return slide;
}

module.exports = { buildArchitectureOverview };
