/**
 * PDF Export Helper Functions
 * Shared formatting utilities for jsPDF document generation
 */

import jsPDF from 'jspdf'
import { SDKState } from './types'
import { LABELS_DE } from './export-types'

// =============================================================================
// FORMATTING HELPERS
// =============================================================================

export function formatDate(date: Date | string | undefined): string {
  if (!date) return '-'
  const d = typeof date === 'string' ? new Date(date) : date
  return d.toLocaleDateString('de-DE', {
    day: '2-digit',
    month: '2-digit',
    year: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
  })
}

// =============================================================================
// PAGE LAYOUT HELPERS
// =============================================================================

export function addHeader(doc: jsPDF, title: string, pageNum: number, totalPages: number): void {
  const pageWidth = doc.internal.pageSize.getWidth()

  // Header line
  doc.setDrawColor(147, 51, 234) // Purple
  doc.setLineWidth(0.5)
  doc.line(20, 15, pageWidth - 20, 15)

  // Title
  doc.setFontSize(10)
  doc.setTextColor(100)
  doc.text(title, 20, 12)

  // Page number
  doc.text(`${LABELS_DE.page} ${pageNum}/${totalPages}`, pageWidth - 40, 12)
}

export function addFooter(doc: jsPDF, state: SDKState): void {
  const pageWidth = doc.internal.pageSize.getWidth()
  const pageHeight = doc.internal.pageSize.getHeight()

  // Footer line
  doc.setDrawColor(200)
  doc.setLineWidth(0.3)
  doc.line(20, pageHeight - 15, pageWidth - 20, pageHeight - 15)

  // Footer text
  doc.setFontSize(8)
  doc.setTextColor(150)
  doc.text(`Tenant: ${state.tenantId} | ${LABELS_DE.generatedAt}: ${formatDate(new Date())}`, 20, pageHeight - 10)
}

// =============================================================================
// CONTENT HELPERS
// =============================================================================

export function addSectionTitle(doc: jsPDF, title: string, y: number): number {
  doc.setFontSize(14)
  doc.setTextColor(147, 51, 234) // Purple
  doc.setFont('helvetica', 'bold')
  doc.text(title, 20, y)
  doc.setFont('helvetica', 'normal')
  return y + 10
}

export function addSubsectionTitle(doc: jsPDF, title: string, y: number): number {
  doc.setFontSize(11)
  doc.setTextColor(60)
  doc.setFont('helvetica', 'bold')
  doc.text(title, 25, y)
  doc.setFont('helvetica', 'normal')
  return y + 7
}

export function addText(doc: jsPDF, text: string, x: number, y: number, maxWidth: number = 170): number {
  doc.setFontSize(10)
  doc.setTextColor(60)
  const lines = doc.splitTextToSize(text, maxWidth)
  doc.text(lines, x, y)
  return y + lines.length * 5
}

export function checkPageBreak(doc: jsPDF, y: number, requiredSpace: number = 40): number {
  const pageHeight = doc.internal.pageSize.getHeight()
  if (y + requiredSpace > pageHeight - 25) {
    doc.addPage()
    return 30
  }
  return y
}
