/**
 * slides/06-middleware.js
 * ----------------------------------------------------------------------------
 * Slide 06 -- Middleware: AgentMiddleware extension
 * Custom middleware pattern.
 */
'use strict';

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

function buildMiddleware(pres, theme) {
  const slide = pres.addSlide();
  helpers.slideBase(slide, pres, theme);
  helpers.addHeader(slide, pres, theme, {
    eyebrow: 'STAGE 4',
    sectionNumber: 4,
    title: 'Middleware: точки расширения',
  });

  helpers.addCodeBlock(slide, pres, theme, {
    x: 0.5, y: layouts.CONTENT_TOP, w: 5.6, h: 3.5,
    language: 'python',
    code: [
      'from langchain.agents.middleware import (',
      '    AgentMiddleware,',
      ')',
      '',
      'class AuditMiddleware(AgentMiddleware):',
      '    def after_model(self, state, runtime):',
      '        runtime.logger.info(',
      '            f"step={state.get(\"step\")}"',
      '        )',
      '        return state',
      '',
      '    def before_model(self, state, runtime):',
      '        return state  # inject reminder',
    ].join('\n'),
    filePath: 'examples/audit_middleware.py',
    startLine: 1,
    highlightLines: [5, 6, 7, 8],
  });

  helpers.addCallout(slide, pres, theme, {
    x: 6.3, y: layouts.CONTENT_TOP, w: 3.2, h: 1.7,
    kind: 'info',
    title: 'Built-in middleware',
    text:
      '* check_message_queue\n' +
      '* notify_step_limit\n' +
      '* open_pr_if_needed\n' +
      '* ToolErrorMiddleware',
  });

  helpers.addCallout(slide, pres, theme, {
    x: 6.3, y: 3.0, w: 3.2, h: 1.8,
    kind: 'success',
    title: 'Типичные кастомы',
    text:
      'Approval gate перед PR, cost ' +
      'guard на длину контекста, ' +
      'redaction секретов в логах.',
  });

  helpers.addPageNumber(slide, pres, theme, 6);
  helpers.addSourceLine(slide, pres, theme, {
    source: 'research/per-tech/openswe.md: 117-136, 213-223',
  });
  return slide;
}

module.exports = { buildMiddleware };
