// Types shared between ReportGenerator and ReportPrintView

export interface ProjectData {
  id: string
  machine_name: string
  machine_type: string
  manufacturer: string
  status: string
  completeness_pct: number
  created_at: string
  updated_at: string
  component_count: number
  hazard_count: number
  mitigation_count: number
}

export interface HazardData {
  id: string
  name: string
  description: string
  component_name: string | null
  category: string
  lifecycle_phase: string
  trigger_event: string
  affected_person: string
  possible_harm: string
  severity: number
  exposure: number
  probability: number
  avoidance: number
  r_inherent: number
  risk_level: string
  status: string
}

export interface MitigationData {
  id: string
  title: string
  description: string
  reduction_type: 'design' | 'protection' | 'information'
  status: 'planned' | 'implemented' | 'verified'
  linked_hazard_ids: string[]
  linked_hazard_names: string[]
}

export interface NormSuggestion {
  norm: {
    id: string
    number: string
    title_de: string
    norm_type: string
    scope_de: string
    mandatory: boolean
  }
  reason: string
  confidence: number
}

export interface NormResult {
  a_norms: NormSuggestion[]
  b1_norms: NormSuggestion[]
  b2_norms: NormSuggestion[]
  c_norms: NormSuggestion[]
  total: number
}

export interface ComplianceTrigger {
  id: string
  regulation: string
  article: string
  title: string
  severity: 'high' | 'medium' | 'low'
  reason: string
  affected_hazard_count?: number
  module_path: string
  module_label: string
}

export interface RiskSummary {
  critical?: number
  high?: number
  medium?: number
  low?: number
  total?: number
}

export interface ReportData {
  project: ProjectData
  hazards: HazardData[]
  mitigations: MitigationData[]
  norms: NormResult | null
  triggers: ComplianceTrigger[]
  riskSummary: RiskSummary
}

// Helpers shared by report views

export function rpz(s: number, e: number, p: number, a: number): number {
  return a >= 1 ? s * e * p * a : s * e * p
}

export function plFromRpz(r: number): string {
  if (r > 300) return 'e'
  if (r >= 151) return 'd'
  if (r >= 61) return 'c'
  if (r >= 21) return 'b'
  return 'a'
}

export function silFromRpz(r: number): number {
  if (r > 300) return 3
  if (r >= 151) return 2
  if (r >= 61) return 1
  return 0
}

export function riskLevelLabel(level: string): string {
  const labels: Record<string, string> = {
    not_acceptable: 'Nicht akzeptabel',
    very_high: 'Sehr hoch',
    critical: 'Kritisch',
    high: 'Hoch',
    medium: 'Mittel',
    low: 'Niedrig',
  }
  return labels[level] || level
}

export function riskLevelColor(level: string): string {
  const colors: Record<string, string> = {
    not_acceptable: '#dc2626',
    very_high: '#dc2626',
    critical: '#dc2626',
    high: '#ea580c',
    medium: '#ca8a04',
    low: '#16a34a',
  }
  return colors[level] || '#6b7280'
}

export const CATEGORY_LABELS: Record<string, string> = {
  mechanical: 'Mechanisch',
  electrical: 'Elektrisch',
  thermal: 'Thermisch',
  pneumatic_hydraulic: 'Pneumatik/Hydraulik',
  noise_vibration: 'Laerm/Vibration',
  ergonomic: 'Ergonomie',
  material_environmental: 'Stoffe/Umwelt',
  software_control: 'Software/Steuerung',
  cyber_network: 'Cyber/Netzwerk',
  ai_specific: 'KI-spezifisch',
}

export const REDUCTION_LABELS: Record<string, string> = {
  design: 'Stufe 1: Design',
  protection: 'Stufe 2: Schutz',
  information: 'Stufe 3: Information',
}

export const STATUS_LABELS: Record<string, string> = {
  planned: 'Geplant',
  implemented: 'Umgesetzt',
  verified: 'Verifiziert',
}
