-- Migration: Backfill delivery_status dla paczek z rozpoznanym raw statusem, ktory do tej pory nie byl zmapowany
-- Powod: DeliveryStatus::normalize() zwracal 'unknown' dla:
--   apaczka  RETURNED_TO_SHIPPER (-> returned)
--   apaczka  PICKUP              (-> in_transit)
--   allegro_wza collected_from_sender (-> in_transit)
-- Po dodaniu tych wpisow do PROVIDER_MAP w kodzie, uspojniamy stan bazy.
-- Idempotentna: UPDATE dotknie tylko wierszy ktore wciaz maja delivery_status='unknown'.

UPDATE shipment_packages
SET delivery_status = 'returned'
WHERE provider = 'apaczka'
  AND delivery_status = 'unknown'
  AND delivery_status_raw = 'RETURNED_TO_SHIPPER';

UPDATE shipment_packages
SET delivery_status = 'in_transit'
WHERE provider = 'apaczka'
  AND delivery_status = 'unknown'
  AND delivery_status_raw = 'PICKUP';

UPDATE shipment_packages
SET delivery_status = 'in_transit'
WHERE provider = 'allegro_wza'
  AND delivery_status = 'unknown'
  AND delivery_status_raw = 'collected_from_sender';
