-- Migration: Map existing numeric order statuses to text values
-- Run this ONCE on the production PostgreSQL database after deploying the updated orders import.
-- This updates ~2.88M rows. On a busy system, consider running during low-traffic hours.
-- The WHERE clause ensures idempotency - only rows with numeric statuses are updated.

UPDATE orders SET status = CASE status
    WHEN '0' THEN 'created'
    WHEN '10' THEN 'unfinished'
    WHEN '15' THEN 'canceled'
    WHEN '16' THEN 'combined'
    WHEN '20' THEN 'placed'
    WHEN '22' THEN 'placed_incomplete'
    WHEN '30' THEN 'canceled'
    WHEN '40' THEN 'awaiting_payment'
    WHEN '50' THEN 'awaiting_products'
    WHEN '55' THEN 'shipping_later'
    WHEN '56' THEN 'shipping_together'
    WHEN '60' THEN 'ready'
    WHEN '61' THEN 'flagged'
    WHEN '62' THEN 'fix_before_pick'
    WHEN '65' THEN 'manual_picking'
    WHEN '70' THEN 'in_pt'
    WHEN '80' THEN 'picked'
    WHEN '90' THEN 'awaiting_shipment'
    WHEN '91' THEN 'remote_wait'
    WHEN '92' THEN 'awaiting_pickup'
    WHEN '93' THEN 'fix_before_ship'
    WHEN '95' THEN 'shipped_confirmed'
    WHEN '100' THEN 'shipped'
    ELSE status
END
WHERE status ~ '^\d+$';  -- Only update rows that still have numeric statuses

-- Verify the migration
SELECT status, COUNT(*) as count
FROM orders
GROUP BY status
ORDER BY count DESC;
