-- Dry-run: show which rows WOULD change under the aisle normalization
-- migration 0015. Safe to run against any database; makes no changes.
--
-- Usage (from the deployment host):
--   psql "$DATABASE_URL" -f backend/scripts/dry_run_aisle_migration.sql

WITH src AS (
  SELECT
    'ingredient'::text    AS tbl,
    id::text              AS id,
    aisle                 AS old_aisle,
    LOWER(COALESCE(aisle, '')) AS key
  FROM ingredient
  WHERE aisle IS NOT NULL
  UNION ALL
  SELECT
    'grocery_item'::text,
    id::text,
    aisle,
    LOWER(COALESCE(aisle, ''))
  FROM grocery_item
  WHERE aisle IS NOT NULL
),
mapped AS (
  SELECT
    tbl, id, old_aisle,
    CASE key
      WHEN 'canned goods' THEN 'Pantry'
      WHEN 'canned'       THEN 'Pantry'
      WHEN 'freezer'      THEN 'Frozen'
      WHEN 'frozen'       THEN 'Frozen'
      WHEN 'produce'      THEN 'Produce'
      WHEN 'fruit'        THEN 'Produce'
      WHEN 'vegetable'    THEN 'Produce'
      WHEN 'dairy'        THEN 'Dairy & Eggs'
      WHEN 'eggs'         THEN 'Dairy & Eggs'
      WHEN 'cheese'       THEN 'Dairy & Eggs'
      WHEN 'milk'         THEN 'Dairy & Eggs'
      WHEN 'yogurt'       THEN 'Dairy & Eggs'
      WHEN 'meat'         THEN 'Meat & Seafood'
      WHEN 'seafood'      THEN 'Meat & Seafood'
      WHEN 'fish'         THEN 'Meat & Seafood'
      WHEN 'chicken'      THEN 'Meat & Seafood'
      WHEN 'beef'         THEN 'Meat & Seafood'
      WHEN 'pork'         THEN 'Meat & Seafood'
      WHEN 'meat_seafood' THEN 'Meat & Seafood'
      WHEN 'bakery'       THEN 'Bakery'
      WHEN 'bread'        THEN 'Bakery'
      WHEN 'beverage'     THEN 'Beverages'
      WHEN 'beverages'    THEN 'Beverages'
      WHEN 'drinks'       THEN 'Beverages'
      WHEN 'spice'        THEN 'Spices'
      WHEN 'spices'       THEN 'Spices'
      WHEN 'seasoning'    THEN 'Spices'
      WHEN 'pantry'       THEN 'Pantry'
      WHEN 'dry'          THEN 'Pantry'
      WHEN 'snack'        THEN 'Pantry'
      WHEN 'snacks'       THEN 'Pantry'
      ELSE 'Other'
    END AS new_aisle
  FROM src
)
SELECT tbl,
       COUNT(*)                AS rows_to_change,
       COUNT(DISTINCT old_aisle) AS distinct_old_values
FROM mapped
WHERE old_aisle IS DISTINCT FROM new_aisle
GROUP BY tbl
ORDER BY tbl;

-- Optional detail dump (uncomment to inspect actual rows):
-- SELECT tbl, old_aisle, new_aisle, COUNT(*)
-- FROM mapped
-- WHERE old_aisle IS DISTINCT FROM new_aisle
-- GROUP BY tbl, old_aisle, new_aisle
-- ORDER BY tbl, old_aisle;
