/**
 * Archetype: imageFullBleed — full-slide image with optional overlay text.
 *
 * Slots:
 *   - imagePath / imageData  (one required) full-bleed image source
 *   - title    (optional) overlay caption — left-aligned at bottom
 *   - eyebrow  (optional) small label above the title
 *   - overlay  (optional) 'dark' | 'light' | 'none' — gradient over
 *              the image so overlay text stays legible
 *
 * If no image is provided the archetype renders a graceful fallback —
 * a single accent-coloured panel with the overlay text — so the deck
 * still builds when the agent's image path is missing.
 */
import { applyChrome, eyebrow, placeText } from './_helpers.js';
import { SLIDE_DIMENSIONS } from '../grid.js';

export const id = 'imageFullBleed';
export const slots = ['imagePath', 'imageData', 'title', 'eyebrow', 'overlay'];

export default function render(slide, resolved, content, _pptx, _ctx) {
  applyChrome(slide, resolved, { bgOverride: 'solid', seed: 67, chrome: false });

  const hasImage = content.imagePath || content.imageData;
  if (hasImage) {
    const imgOpts = {
      x: 0, y: 0,
      w: SLIDE_DIMENSIONS.width,
      h: SLIDE_DIMENSIONS.height,
      sizing: { type: 'cover', w: SLIDE_DIMENSIONS.width, h: SLIDE_DIMENSIONS.height },
    };
    if (content.imagePath) imgOpts.path = content.imagePath;
    if (content.imageData) imgOpts.data = content.imageData;
    slide.addImage(imgOpts);
  } else {
    // Fallback: solid accent panel so overlay text still reads.
    slide.addShape('rect', {
      x: 0, y: 0,
      w: SLIDE_DIMENSIONS.width, h: SLIDE_DIMENSIONS.height,
      fill: { color: resolved.palette.primary },
      line: { type: 'none' },
    });
  }

  // Bottom overlay — dark gradient on light overlay, light on dark.
  const overlayMode = content.overlay || (hasImage ? 'dark' : 'none');
  if (overlayMode !== 'none') {
    const overlayColor = overlayMode === 'light' ? 'FFFFFF' : '000000';
    slide.addShape('rect', {
      x: 0,
      y: SLIDE_DIMENSIONS.height * 0.55,
      w: SLIDE_DIMENSIONS.width,
      h: SLIDE_DIMENSIONS.height * 0.45,
      fill: { color: overlayColor, transparency: 60 },
      line: { type: 'none' },
    });
  }

  const textColor = overlayMode === 'light' ? resolved.palette.text : 'FFFFFF';
  const overlayY = SLIDE_DIMENSIONS.height - resolved.spacing.pageMargin - 1.5;

  if (content.eyebrow) {
    eyebrow(slide, resolved, content.eyebrow, {
      x: resolved.spacing.safe.x,
      y: overlayY,
      w: resolved.spacing.safe.w,
      color: resolved.palette.accent,
    });
  }

  if (content.title) {
    placeText(slide, resolved, 'title', content.title, {
      x: resolved.spacing.safe.x,
      y: overlayY + 0.3,
      w: resolved.spacing.safe.w * 0.85,
      h: 1.1,
    }, { color: textColor, align: 'left' });
  }
}
