/**
 * Archetype: closing — final slide. Thank-you, CTA, contact.
 *
 * Slots:
 *   - eyebrow  (optional) "Thank you." or a similar sign-off label
 *   - title    (required) the closing message ("Let's build this together.")
 *   - cta      (optional) {label, url} — a single emphatic line
 *   - contact  (optional) [{label, value}] — array of contact rows
 *
 * Visual: large title, accent-color CTA, contact rows as a small
 * justified list at the bottom. Always uses the theme default
 * background unless overridden — the closing slide should match the
 * hero in mood.
 */
import { applyChrome, eyebrow, placeText, rule } from './_helpers.js';
import { textStyle } from '../typography.js';

export const id = 'closing';
export const slots = ['eyebrow', 'title', 'cta', 'contact'];

export default function render(slide, resolved, content, _pptx, _ctx) {
  applyChrome(slide, resolved, { bgOverride: content.background, seed: 83, chrome: false });

  const { safe } = resolved.spacing;
  const titleY = safe.y + safe.h * 0.36;

  if (content.eyebrow) {
    eyebrow(slide, resolved, content.eyebrow, {
      x: safe.x, y: titleY - 0.45, w: safe.w / 2,
      color: resolved.palette.accent,
    });
  }

  placeText(slide, resolved, 'display', content.title, {
    x: safe.x, y: titleY, w: safe.w * 0.85, h: 2,
  }, {
    color: resolved.palette.text,
    align: 'left',
    fit: 'shrink',
  });

  if (content.cta) {
    const ctaY = titleY + 2.1;
    rule(slide, safe.x, ctaY, 0.6, resolved.palette.primary, 1.2);
    const ctaText = content.cta.label || content.cta.url || String(content.cta);
    slide.addText(ctaText, {
      x: safe.x, y: ctaY + 0.1, w: safe.w * 0.8, h: 0.5,
      ...textStyle(resolved.typography, 'subtitle', {
        color: resolved.palette.primary,
        align: 'left',
        bold: true,
      }),
      hyperlink: content.cta.url ? { url: content.cta.url } : undefined,
    });
  }

  if (Array.isArray(content.contact) && content.contact.length) {
    const rowsY = safe.y + safe.h - 0.9;
    const colW = safe.w / Math.max(content.contact.length, 1);
    content.contact.forEach((row, i) => {
      const x = safe.x + i * colW;
      placeText(slide, resolved, 'caption', row.label || '', {
        x, y: rowsY, w: colW - 0.2, h: 0.3,
      }, { color: resolved.palette.subtle, align: 'left' });
      placeText(slide, resolved, 'body', row.value || '', {
        x, y: rowsY + 0.3, w: colW - 0.2, h: 0.4,
      }, { color: resolved.palette.text, align: 'left' });
    });
  }
}
