-- Rewritten after this migration failed on a database that already had
-- seeded ingredient rows: the original version (auto-generated by `prisma
-- migrate diff`) cast every existing `category` value directly from the
-- old 18-value enum to the new 7-value one, which fails for every row
-- since none of the old values exist in the new enum. This version instead
-- adds the new columns with a safe default (never casting existing data),
-- then swaps them in — the same "add with a default, correct for real on
-- the next seed run" pattern already used for `IngredientCategory`'s and
-- `IngredientSubcategory`'s own `@default(...)` (see their doc comments in
-- schema.prisma). `seedReferenceData()` runs right after `migrate deploy`
-- on every container start (see apps/api/Dockerfile) and corrects every
-- row's real category/subcategory immediately.
--
-- `DROP TYPE IF EXISTS "IngredientSubcategory"` guards against a previous
-- failed attempt at this exact migration: that CREATE TYPE statement runs
-- outside the AlterEnum transaction below and so persists even though the
-- rest of that failed attempt rolled back — retrying without this guard
-- would hit "type already exists".

-- CreateEnum
DROP TYPE IF EXISTS "IngredientSubcategory";
CREATE TYPE "IngredientSubcategory" AS ENUM ('LEGUMES', 'FRUITS', 'HERBES_FRAICHES', 'VIANDES', 'VOLAILLES', 'POISSONS', 'CRUSTACES_FRUITS_DE_MER', 'FECULENTS', 'LEGUMINEUSES', 'GRAINES_FRUITS_SECS', 'AUTRES', 'PAINS', 'PATES_A_CUIRE', 'PRODUITS_LAITIERS', 'OEUFS', 'ALTERNATIVES', 'EPICES', 'SAUCES', 'ASSAISONNEMENTS', 'BASES', 'EPAISSISSANTS', 'SUCRES');

-- CreateEnum
DROP TYPE IF EXISTS "IngredientCategory_new";
CREATE TYPE "IngredientCategory_new" AS ENUM ('PRODUITS_FRAIS', 'BOUCHERIE_POISSONNERIE', 'EPICERIE_SECHE', 'BOULANGERIE', 'CREMERIE_FROMAGE', 'CONDIMENTS_EPICES', 'AIDES_CULINAIRES');

-- AlterTable: add the new columns at their defaults — no cast of existing
-- `category` values, so this succeeds regardless of what the table
-- currently holds.
ALTER TABLE "ingredients" ADD COLUMN "category_new" "IngredientCategory_new" NOT NULL DEFAULT 'EPICERIE_SECHE';
ALTER TABLE "ingredients" ADD COLUMN "subcategory" "IngredientSubcategory" NOT NULL DEFAULT 'AUTRES';

-- Swap the old `category` column (old 18-value enum) out for the new one.
ALTER TABLE "ingredients" DROP COLUMN "category";
ALTER TABLE "ingredients" RENAME COLUMN "category_new" TO "category";
DROP TYPE "IngredientCategory";
ALTER TYPE "IngredientCategory_new" RENAME TO "IngredientCategory";
