/**
 * pres/master — slide master + deck-level context.
 *
 * pptxgenjs's `defineSlideMaster` lets us put recurring chrome (bg
 * colour, static brand strip, page-number placeholder, decorative
 * accent shapes) in one place so the file size shrinks and every
 * slide stays in lockstep visually. The dynamic per-slide chrome
 * (section name + slide-number text + section progress dots) still
 * lives on the slide itself — pptxgenjs's master placeholders can't
 * read per-slide data.
 *
 * The deck context is computed once per build: it tracks which
 * section each slide belongs to (by walking the slide list and
 * resetting on every sectionHeader archetype) so the chrome can show
 * "01 / 04" indicators without the agent labelling each slide.
 */
import { SLIDE_DIMENSIONS } from './grid.js';

/** Archetypes that intentionally suppress header/footer chrome. */
const BARE_ARCHETYPES = new Set([
  'hero', 'sectionHeader', 'closing', 'imageFullBleed',
  'headlineMessage', 'productHero',
]);

/** True iff this archetype should render with the chrome master. */
export function usesChrome(archetypeId) {
  return !BARE_ARCHETYPES.has(archetypeId);
}

/**
 * Define one slide master per theme. We register two variants:
 *   LOXIA_BARE   — used by hero / section / closing / imageFullBleed
 *                  (background only; archetype owns the entire canvas)
 *   LOXIA_CHROME — used by everything else; same background + an
 *                  ultra-thin accent stripe at top (the dynamic
 *                  section name + page number sit on the slide via
 *                  headerChrome/footerChrome).
 *
 * Returns the names so the caller can pick the right one per slide.
 */
export function defineMasters(pptx, resolved) {
  const palette = resolved.palette;

  pptx.defineSlideMaster({
    title: 'LOXIA_BARE',
    background: { color: palette.bg },
    objects: [],
  });

  pptx.defineSlideMaster({
    title: 'LOXIA_CHROME',
    background: { color: palette.bg },
    objects: [
      // Top accent hairline — the single piece of static chrome the
      // master can place. Section name + page number are dynamic and
      // are added per slide.
      { rect: { x: 0, y: 0, w: SLIDE_DIMENSIONS.width, h: 0.04, fill: { color: palette.primary } } },
    ],
  });

  return { BARE: 'LOXIA_BARE', CHROME: 'LOXIA_CHROME' };
}

/**
 * Pre-compute a context for every slide so archetypes can render
 * section progress / page numbers without the agent labelling each
 * slide manually.
 *
 *   { slides: [...originalSlides], context: [{slideIndex, totalSlides,
 *     sectionName, sectionIdx, sectionTotal, brand, isSectionStart}] }
 */
export function buildDeckContext(spec) {
  const slides = Array.isArray(spec.slides) ? spec.slides : [];
  const totalSlides = slides.length;

  // First pass: collect sections (sectionHeader slides) so we know
  // how many total + what their names are.
  const sections = [];
  slides.forEach((s, i) => {
    if (s.archetype === 'sectionHeader') {
      sections.push({ idx: sections.length, slideIndex: i, name: s.title || `Section ${sections.length + 1}` });
    }
  });
  const sectionTotal = sections.length;

  // Second pass: assign each slide to a section based on which
  // sectionHeader came before it.
  const context = [];
  let currentSectionIdx = -1;
  for (let i = 0; i < slides.length; i++) {
    if (slides[i].archetype === 'sectionHeader') {
      currentSectionIdx++;
    }
    const sectionName = currentSectionIdx >= 0 ? sections[currentSectionIdx].name : null;
    context.push({
      slideIndex: i,
      totalSlides,
      sectionName,
      sectionIdx: Math.max(0, currentSectionIdx),
      sectionTotal,
      brand: spec.company || spec.title || '',
      isSectionStart: slides[i].archetype === 'sectionHeader',
    });
  }

  return { slides, context };
}
