/**
 * Archetype: twoColChart — text on the left, chart on the right.
 *
 * Slots:
 *   - title       (optional) header text
 *   - leftText  { headline, body }   (required)
 *   - chart     { type, ... }        (required) — see pres/charts
 *
 * tech-bold puts the chart inside a surface-coloured card with soft
 * shadow; editorial and data-heavy leave it inline.
 */
import { applyChrome, callout, placeText } from './_helpers.js';
import { resolveChart } from '../charts/index.js';

export const id = 'twoColChart';
export const slots = ['title', 'leftText', 'chart'];

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

  const cfg = resolved.archetypeConfig('twoColChart');
  const { header, body, col } = resolved.spacing;

  placeText(slide, resolved, 'title', content.title, {
    x: header.x, y: header.y, w: header.w, h: 0.7,
  }, { color: resolved.palette.text, align: 'left' });

  // Left text column — 5/12 of the grid.
  const left = { ...col(1, 5), y: body.y, h: body.h };
  if (content.leftText) {
    placeText(slide, resolved, 'subtitle', content.leftText.headline, {
      x: left.x, y: left.y, w: left.w, h: 0.6,
    }, { color: resolved.palette.primary, align: 'left', bold: true });

    placeText(slide, resolved, 'body', content.leftText.body, {
      x: left.x, y: left.y + 0.7, w: left.w, h: left.h - 0.7,
    }, { color: resolved.palette.text, align: 'left', valign: 'top' });
  }

  // Right chart column — 7/12 of the grid.
  const right = { ...col(6, 7), y: body.y, h: body.h };

  if (cfg.chartElevated) {
    callout(slide, resolved, right, resolved.palette.surface);
  }

  const chartSpec = resolveChart(content.chart, resolved);
  if (chartSpec && pptx) {
    const padding = resolved.spacing.blockPadding;
    const chartFrame = {
      x: right.x + (cfg.chartElevated ? padding : 0),
      y: right.y + (cfg.chartElevated ? padding : 0),
      w: right.w - (cfg.chartElevated ? padding * 2 : 0),
      h: right.h - (cfg.chartElevated ? padding * 2 : 0),
    };
    slide.addChart(pptx.ChartType[chartSpec.type] || chartSpec.type, chartSpec.data, {
      ...chartFrame,
      ...chartSpec.opts,
    });
  }
}
