// =============================================================================
// TOM Generator PDF Export - Helper Functions
// Private helpers extracted from pdf.ts to stay under 500 LOC hard cap
// =============================================================================

import { DerivedTOM, CONTROL_CATEGORIES } from '../types'
import { getControlById } from '../controls/loader'
import type { PDFExportOptions } from './pdf'

// =============================================================================
// HELPER FUNCTIONS
// =============================================================================

export function generateCategorySummary(
  toms: DerivedTOM[],
  opts: PDFExportOptions
): string[][] {
  const summary: string[][] = []

  for (const category of CONTROL_CATEGORIES) {
    const categoryTOMs = toms.filter((tom) => {
      const control = getControlById(tom.controlId)
      return control?.category === category.id
    })

    if (categoryTOMs.length === 0) continue

    const required = categoryTOMs.filter((t) => t.applicability === 'REQUIRED').length
    const implemented = categoryTOMs.filter((t) => t.implementationStatus === 'IMPLEMENTED').length

    summary.push([
      category.name[opts.language],
      String(categoryTOMs.length),
      String(required),
      String(implemented),
    ])
  }

  return summary
}

export function formatProtectionLevel(level: string, language: 'de' | 'en'): string {
  const levels: Record<string, Record<'de' | 'en', string>> = {
    NORMAL: { de: 'Normal', en: 'Normal' },
    HIGH: { de: 'Hoch', en: 'High' },
    VERY_HIGH: { de: 'Sehr hoch', en: 'Very High' },
  }
  return levels[level]?.[language] || level
}

export function formatType(type: string, language: 'de' | 'en'): string {
  const types: Record<string, Record<'de' | 'en', string>> = {
    TECHNICAL: { de: 'Technisch', en: 'Technical' },
    ORGANIZATIONAL: { de: 'Organisatorisch', en: 'Organizational' },
  }
  return types[type]?.[language] || type
}

export function formatImplementationStatus(status: string, language: 'de' | 'en'): string {
  const statuses: Record<string, Record<'de' | 'en', string>> = {
    NOT_IMPLEMENTED: { de: 'Nicht umgesetzt', en: 'Not Implemented' },
    PARTIAL: { de: 'Teilweise', en: 'Partial' },
    IMPLEMENTED: { de: 'Umgesetzt', en: 'Implemented' },
  }
  return statuses[status]?.[language] || status
}

export function formatApplicability(applicability: string, language: 'de' | 'en'): string {
  const apps: Record<string, Record<'de' | 'en', string>> = {
    REQUIRED: { de: 'Erforderlich', en: 'Required' },
    RECOMMENDED: { de: 'Empfohlen', en: 'Recommended' },
    OPTIONAL: { de: 'Optional', en: 'Optional' },
    NOT_APPLICABLE: { de: 'N/A', en: 'N/A' },
  }
  return apps[applicability]?.[language] || applicability
}

export function getCIAMeaning(rating: number, language: 'de' | 'en'): string {
  const meanings: Record<number, Record<'de' | 'en', string>> = {
    1: { de: 'Sehr gering', en: 'Very Low' },
    2: { de: 'Gering', en: 'Low' },
    3: { de: 'Mittel', en: 'Medium' },
    4: { de: 'Hoch', en: 'High' },
    5: { de: 'Sehr hoch', en: 'Very High' },
  }
  return meanings[rating]?.[language] || String(rating)
}
