/**
 * Table Summary Section Builder
 *
 * Creates a summary table page with all automations and totals.
 * Consumer provides content only; styling is handled internally.
 */

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

/**
 * Creates a summary table page with automation overview and totals
 *
 * @param config - Content-only configuration
 * @returns Page object ready for PDFDocument.pages array
 */
export function tableSummarySection(config: TableSummaryConfig): Page {
  const { automations } = config

  // Calculate totals
  const totalHoursPerWeek = automations.reduce((sum, a) => sum + a.hoursPerWeek, 0)
  const totalAnnualSavings = automations.reduce((sum, a) => sum + a.annualSavings, 0)

  // Format table rows - show full titles
  const tableRows = automations.map(a => [
    a.title,
    String(a.hoursPerWeek),
    `$${a.annualSavings.toLocaleString()}`
  ])

  return {
    sections: [
      {
        type: 'table',
        headers: ['Automation', 'Hours/Week', 'Annual Savings'],
        rows: tableRows,
        columnWidths: ['*', 80, 100]
      },
      {
        type: 'card',
        layout: 'horizontal',
        content: [
          {
            type: 'metric',
            value: String(totalHoursPerWeek),
            label: 'Total Hours/Week Saved'
          },
          {
            type: 'metric',
            value: `$${totalAnnualSavings.toLocaleString()}`,
            label: 'Total Annual Savings'
          }
        ]
      }
    ],
    header: { title: 'Summary', variant: 'accent' },
    footer: true
  }
}
