/**
 * Archetype: hero — title slide opening a deck.
 *
 * Slots:
 *   - eyebrow   (optional) small uppercase label above title
 *   - title     (required) huge display text — the deck's name
 *   - subtitle  (optional) supporting line below title
 *   - author    (optional) presenter name in footer
 *   - date      (optional) date string in footer
 *   - background (optional) per-slide bg override; defaults to theme.bg
 *
 * Layout is left-aligned by default; theme `archetypes.hero.align`
 * can switch to 'center'. Tech-bold likes the eyebrow rendered in
 * accent colour; editorial puts a hairline rule under the eyebrow.
 */
import { applyChrome, eyebrow, placeText, rule } from './_helpers.js';

export const id = 'hero';
export const slots = ['eyebrow', 'title', 'subtitle', 'author', 'date'];

export default function render(slide, resolved, content, _pptx, _ctx) {
  // Hero opts out of the chrome master — clean canvas for the title.
  applyChrome(slide, resolved, { bgOverride: content.background, seed: 11, chrome: false });

  const cfg = resolved.archetypeConfig('hero');
  const align = cfg.align || 'left';
  const { safe, body } = resolved.spacing;

  // Title block roughly vertically centered in the body region but
  // weighted up slightly so subtitle + footer have breathing room.
  const titleY = safe.y + safe.h * 0.32;
  const titleFrame = { x: safe.x, y: titleY, w: safe.w, h: 2 };

  if (content.eyebrow) {
    const ebColor = cfg.eyebrowOnAccent ? resolved.palette.accent : resolved.palette.primary;
    eyebrow(slide, resolved, content.eyebrow, {
      x: safe.x, y: titleY - 0.5, w: safe.w / 2, color: ebColor,
    });
    if (cfg.eyebrowDivider) {
      rule(slide, safe.x, titleY - 0.18, 0.6, ebColor, 1);
    }
  }

  placeText(slide, resolved, 'display', content.title, titleFrame, {
    color: resolved.palette.text,
    align,
    valign: 'top',
    fit: 'shrink',
  });

  if (content.subtitle) {
    placeText(slide, resolved, 'subtitle', content.subtitle, {
      x: safe.x, y: titleY + 1.9, w: safe.w * 0.78, h: 1.2,
    }, {
      color: resolved.palette.subtle,
      align,
    });
  }

  // Footer: author · date, on a single line, subtle colour, hairline above.
  const footerY = resolved.spacing.slide.height - resolved.spacing.pageMargin - 0.5;
  if (content.author || content.date) {
    const parts = [content.author, content.date].filter(Boolean).join('  ·  ');
    placeText(slide, resolved, 'caption', parts, {
      x: safe.x, y: footerY, w: safe.w, h: 0.3,
    }, { color: resolved.palette.subtle, align: 'left' });

    rule(slide, safe.x, footerY - 0.08, 0.6, resolved.palette.primary, 1);
  }
}
