/**
 * Summary Section Builder
 *
 * Creates an executive summary page with key metrics and description.
 * Consumer provides content only; styling is handled internally.
 */

import type { Page } from '../types'
import type { SummaryConfig } from './types'

/**
 * Creates an executive summary page with metrics card and description
 *
 * Supports paragraph breaks: description can contain \n\n for paragraph separation.
 *
 * @param config - Content-only configuration
 * @returns Page object ready for PDFDocument.pages array
 */
export function summarySection(config: SummaryConfig): Page {
  const { totalSavings, automationCount, description } = config

  // Split description into paragraphs (double newline = paragraph break)
  const paragraphs = description.split(/\n\n+/).filter(p => p.trim())

  // Build description sections - each paragraph as separate text block
  const descriptionSections = paragraphs.map(paragraph => ({
    type: 'text' as const,
    content: paragraph.trim(),
    variant: 'body' as const
  }))

  return {
    sections: [
      {
        type: 'card',
        layout: 'horizontal',
        content: [
          {
            type: 'metric',
            value: `$${totalSavings.toLocaleString()}`,
            label: 'Projected Annual Savings'
          },
          {
            type: 'metric',
            value: String(automationCount),
            label: 'Automations Proposed'
          }
        ]
      },
      ...descriptionSections
    ],
    header: { title: 'Executive Summary', variant: 'accent' },
    footer: true
  }
}
