/**
 * slides/14-triggers-thread-id.js
 * ----------------------------------------------------------------------------
 * Slide 14 -- Trigger routing + deterministic thread_id
 */
'use strict';

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

function buildTriggerRouting(pres, theme) {
  const slide = pres.addSlide();
  helpers.slideBase(slide, pres, theme);
  helpers.addHeader(slide, pres, theme, {
    eyebrow: 'STAGE 4',
    sectionNumber: 4,
    title: 'Triggers: thread_id routing',
  });

  helpers.addCodeBlock(slide, pres, theme, {
    x: 0.5, y: layouts.CONTENT_TOP, w: 5.6, h: 3.5,
    language: 'python',
    code: [
      '# open_swe/triggers/router.py',
      'def thread_id_for(source, ref, comment_id):',
      '    # Deterministic thread_id:',
      '    # все follow-up попадают в один run.',
      '    if source == "slack":',
      '        return f"slack:{ref}"',
      '    if source == "linear":',
      '        return f"linear:{ref}"',
      '    if source == "github":',
      '        return f"github:{ref}:{comment_id}"',
      '    raise ValueError(source)',
    ].join('\n'),
    filePath: 'open_swe/triggers/router.py',
    startLine: 1,
    highlightLines: [2, 3, 4, 5, 6, 7, 8, 9],
  });

  helpers.addCallout(slide, pres, theme, {
    x: 6.3, y: layouts.CONTENT_TOP, w: 3.2, h: 1.55,
    kind: 'info',
    title: 'Routing',
    text:
      'thread_id из (source, ref). ' +
      'Если run бежит -- новые ' +
      'сообщения ждут в очереди.',
  });

  helpers.addCallout(slide, pres, theme, {
    x: 6.3, y: 3.05, w: 3.2, h: 1.8,
    kind: 'warning',
    title: 'Concurrency',
    text:
      'Один thread = один run. ' +
      'Параллельность через ' +
      'отдельный thread_id ' +
      '(например, отдельный Slack).',
  });

  helpers.addPageNumber(slide, pres, theme, 14);
  helpers.addSourceLine(slide, pres, theme, {
    source: 'research/per-tech/openswe.md: 96-97, 131-133',
  });
  return slide;
}

module.exports = { buildTriggerRouting };
