/**
 * Summary + Investment Combined Section Builder
 *
 * Creates a single page combining the summary table with investment details.
 * Consolidates two sparse pages into one efficient layout.
 *
 * Layout:
 * - Summary table (automations with hours/savings)
 * - Totals metrics card
 * - Projection disclaimer
 * - Next steps
 * - Contact CTA
 */

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

/** Savings rate used for projections (matches ROI calculator) */
const SAVINGS_RATE_PERCENT = 70

/** Default next steps if not provided */
const DEFAULT_NEXT_STEPS = [
  'Review this proposal and note any questions',
  'Schedule a follow-up call to discuss details',
  "We'll provide a detailed SOW with timeline",
  'Begin implementation within 2 weeks of approval'
]

/**
 * Creates a combined summary and investment page
 *
 * This consolidates what was previously two separate pages:
 * - Table summary (automation list with metrics)
 * - Investment (tier + next steps)
 *
 * @param config - Content-only configuration
 * @returns Page object ready for PDFDocument.pages array
 */
export function summaryInvestmentSection(config: SummaryInvestmentConfig): Page {
  const {
    automations,
    initialFee,
    monthlyFee,
    nextSteps = DEFAULT_NEXT_STEPS,
    contactCta = 'Questions? Reply to this email or use the contact link provided by your advisor'
  } = 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
  const tableRows = automations.map(a => [
    a.title,
    String(a.hoursPerWeek),
    `$${a.annualSavings.toLocaleString()}`
  ])

  // Footer row with totals
  const footerRow = [
    'Projected Savings',
    String(totalHoursPerWeek),
    `$${totalAnnualSavings.toLocaleString()}`
  ]

  // Build pricing metrics (always show initial fee, conditionally show monthly fee)
  const pricingMetrics: Array<{ type: 'metric'; value: string; label: string }> = [
    {
      type: 'metric',
      value: `$${initialFee.toLocaleString()}`,
      label: 'Initial Fee'
    }
  ]

  if (monthlyFee) {
    pricingMetrics.push({
      type: 'metric',
      value: `$${monthlyFee.toLocaleString()}/mo`,
      label: 'Monthly Fee'
    })
  }

  return {
    sections: [
      // Summary table with totals row
      {
        type: 'table',
        headers: ['Automation', 'Hrs/Week Saved', 'Proj. Annual Savings'],
        rows: tableRows,
        footerRow,
        columnWidths: ['*', 100, 120]
      },
      // Projection disclaimer
      {
        type: 'text',
        content: `*Projections based on ${SAVINGS_RATE_PERCENT}% efficiency improvement, a conservative estimate for automation ROI. Actual results may vary based on implementation specifics.`,
        variant: 'caption'
      },
      // Price label
      { type: 'text', content: 'Price', variant: 'subtitle' },
      // Pricing metrics
      {
        type: 'card',
        layout: 'horizontal',
        content: pricingMetrics
      },
      // Next steps
      { type: 'text', content: 'Next Steps', variant: 'subtitle' },
      {
        type: 'list',
        ordered: true,
        items: nextSteps
      },
      // CTA
      {
        type: 'text',
        content: contactCta,
        variant: 'caption'
      }
    ],
    header: { title: 'Summary & Investment', variant: 'accent' },
    footer: true
  }
}
