-- Migration 046: FDDB-Systemvorlage — kcal ohne legacy conversion_factor/target_unit (Issue #21, source_unit-Registry)
-- Doppelte Umrechnung (kj-Faktor * 0.239) wurde fälschlich gespeichert; target_unit ist nur für duration vorgesehen.

UPDATE csv_field_mappings
SET type_conversions = jsonb_set(
    type_conversions,
    '{kcal}',
    ((type_conversions->'kcal') - 'conversion_factor' - 'target_unit')
        || jsonb_build_object('source_unit', 'kj')
)
WHERE is_system = true
  AND profile_id IS NULL
  AND module = 'nutrition'
  AND mapping_name = 'FDDB Export (Standard)'
  AND (type_conversions->'kcal') IS NOT NULL
  AND (
      (type_conversions->'kcal') ? 'conversion_factor'
      OR (type_conversions->'kcal') ? 'target_unit'
  );
