// vsm-engine — shared types (Wertstrom P1, KAR-878/KAR-986).
//
// Every exported metric in this module carries a `MetricExplain` — same
// "Formula + Datenbasis + Ausschluss-Liste" contract components/wertstrom/
// vsm-metrics.ts established (itself modeled on lib/qaf-differences/internal/
// explain-provenance.ts's Tri-State-Disziplin). Defined fresh here (not
// imported from vsm-metrics.ts) so lib/vsm-engine stays independent of the
// components/ layer — see README.md "Architecture decision".

import type { VsmNode } from '@/lib/vsm-types'

export interface MetricExplain {
  formula: string
  dataBasis: string
  exclusions: string[]
}

/** Shared "is this a capacity/bottleneck-relevant station" predicate —
 * `type==='process'` OR `type==='machine'`. `machine` is a full-fledged
 * station type in the editor (its own icon/label, and the ONLY type with an
 * OEE input field — see `vsm-editor.tsx`), not a sub-case of `process`; a
 * filter that only ever checked `type==='process'` silently dropped every
 * machine node from bottleneck/capacity/time-plausibility analysis (F1/F13
 * review findings). Centralized here so bottleneck.ts and validation.ts
 * can't drift apart on this again. */
export function isStationNode(n: Pick<VsmNode, 'type'>): boolean {
  return n.type === 'process' || n.type === 'machine'
}

/** Rounds to 1 decimal AND renders with a German decimal comma (+ thousands
 * `.` grouping), for German-language explain/reason TEXT only — the actual
 * numeric result fields across this module always stay full-precision raw
 * numbers (rounding/formatting is display policy, left to the caller).
 * Every call site interpolates the result directly into a German sentence
 * template (never does further arithmetic on it), so returning a string
 * unconditionally — instead of a raw JS `number` whose `toString()` always
 * uses a '.' decimal separator — is safe here and fixes every §18/Engpass-
 * Erklärungs-Satz in one place rather than patching each call site
 * (repo-wide "deutsches Zahlenformat" doctrine, e.g. vsm-editor.tsx's own
 * `toLocaleString('de-DE')` calls / lib/copilot-export's `formatNumberDe`). */
export function formatRound1(n: number | null | undefined): string {
  return n == null ? 'unbekannt' : new Intl.NumberFormat('de-DE', { maximumFractionDigits: 1 }).format(n)
}
