/**
 * Archetype: quote — pull-quote with attribution.
 *
 * Slots:
 *   - quote        (required) the quoted text
 *   - attribution  (optional) source / speaker
 *   - subtext      (optional) extra context shown beneath attribution
 *
 * Editorial wraps the quote in giant serif quote marks; tech-bold and
 * data-heavy use a tighter inline mark + accent rule.
 */
import { applyChrome, placeText, rule } from './_helpers.js';
import { textStyle } from '../typography.js';

export const id = 'quote';
export const slots = ['quote', 'attribution', 'subtext'];

export default function render(slide, resolved, content, _pptx, ctx) {
  applyChrome(slide, resolved, { bgOverride: content.background, seed: 59, ctx });

  const cfg = resolved.archetypeConfig('quote');
  const { safe } = resolved.spacing;

  // Optional huge serif quote glyph in the top-left — editorial-style.
  if (cfg.bigQuotes) {
    slide.addText('"', {
      x: safe.x, y: safe.y - 0.4, w: 2, h: 2,
      ...textStyle(resolved.typography, 'display', {
        color: resolved.palette.primary,
        fontSize: 180,
        align: 'left',
        valign: 'top',
      }),
      transparency: 40,
    });
  }

  // The quote — slightly narrower than safe width for a "column-of-text"
  // editorial feel.
  const quoteX = safe.x + safe.w * 0.05;
  const quoteW = safe.w * 0.85;
  const quoteY = safe.y + safe.h * 0.22;
  const quoteRole = cfg.bigQuotes ? 'title' : 'subtitle';

  placeText(slide, resolved, quoteRole, content.quote, {
    x: quoteX, y: quoteY, w: quoteW, h: safe.h * 0.55,
  }, {
    color: resolved.palette.text,
    italic: cfg.bigQuotes,
    align: 'left',
    fontSize: cfg.bigQuotes ? 32 : 28,
  });

  // Hairline above attribution.
  const attrY = safe.y + safe.h * 0.84;
  if (content.attribution) {
    if (cfg.attributionRule) {
      rule(slide, quoteX, attrY - 0.15, 0.6, resolved.palette.primary, 1);
    }
    placeText(slide, resolved, 'subtitle', `— ${content.attribution}`, {
      x: quoteX, y: attrY, w: quoteW, h: 0.5,
    }, { color: resolved.palette.subtle, align: 'left', italic: false });
  }

  if (content.subtext) {
    placeText(slide, resolved, 'caption', content.subtext, {
      x: quoteX, y: attrY + 0.4, w: quoteW, h: 0.3,
    }, { color: resolved.palette.subtle, align: 'left' });
  }
}
