// Paket types scanned at commissioning (CODE1…CODE6 barcodes) → the M_Product record
// iDempiere stores in M_InOut.M_Paket_Type_ID (the column is a Search reference on
// m_product, NOT a separate table). Names/hints mirror the prod products so operators
// see "Paket S" instead of "CODE1" everywhere; the ids are the same hardcoded ones the
// commission pages, ProductForm.vue and the DHL routes have always used.
export interface PaketTypeDef {
  id: number
  code: string
  label: string
  hint: string
  /** Test article (CODETEST) — selectable on the commission page, never offered in the modal */
  test?: boolean
}

export const PAKET_TYPE_CATALOG: PaketTypeDef[] = [
  { id: 1000204, code: 'CODE1', label: 'Paket S', hint: '< 6 Liter' },
  { id: 1000205, code: 'CODE2', label: 'Paket M', hint: '< 15 Liter' },
  { id: 1000206, code: 'CODE3', label: 'Paket L', hint: '< 30 Liter' },
  { id: 1000213, code: 'CODE4', label: 'Paket XL', hint: '< 60 Liter' },
  { id: 1000857, code: 'CODE5', label: 'Keine Verpackung', hint: 'Versand ohne Umverpackung' },
  { id: 1000911, code: 'CODE6', label: 'Kleinpaket', hint: 'bis 1 kg · unter 20 € · nur DE' },
  { id: 1000001, code: 'CODETEST', label: 'Test', hint: 'Testartikel – keine echte Sendung', test: true }
]

export const paketTypeById = (id: unknown): PaketTypeDef | undefined =>
  PAKET_TYPE_CATALOG.find(p => String(p.id) === String(id ?? ''))

/** "Paket S (CODE1)" — falls back to the raw id so unknown values stay visible */
export const paketTypeLabel = (id: unknown): string => {
  const def = paketTypeById(id)
  return def ? `${def.label} (${def.code})` : (id === '' || id === null || id === undefined ? '' : String(id))
}

export const usePaketTypes = (opts: { includeTest?: boolean } = {}) => {
  const includeTest = opts.includeTest ?? true
  const options = computed(() => PAKET_TYPE_CATALOG
    .filter(p => includeTest || !p.test)
    .map(p => ({ ...p, title: `${p.label}`, subtitle: `${p.code} · ${p.hint}` })))
  return { catalog: PAKET_TYPE_CATALOG, options, byId: paketTypeById, labelFor: paketTypeLabel }
}
