-- Adds the `unit` reference catalog (key/type/to_base_factor) so recipe
-- ingredient units are a closed, normalized set instead of free text —
-- groundwork for a future unit-conversion feature (e.g. a shopping list
-- summing "500g" + "0.5kg" of the same ingredient), not that feature
-- itself. Seeded by reference-seed-data.ts's UNITS, same "SQL creates the
-- shape, application code seeds the rows" split as Diet/Allergy/Ingredient.
--
-- `recipe_ingredient.unit` (free text) is replaced by `unit_id` (FK), with
-- no backfill: a free-text value like "cas" or "grammes" can't be reliably
-- mapped to a catalog key without a human in the loop. Acceptable as a
-- straight breaking change here — the app has no real recipes yet
-- (pre-launch) — rather than staging `unit_id` as nullable across a
-- transition nothing will ever populate.
/*
  Warnings:

  - You are about to drop the column `unit` on the `recipe_ingredient` table. All the data in the column will be lost.
  - Added the required column `unit_id` to the `recipe_ingredient` table without a default value. This is not possible if the table is not empty.

*/
-- CreateEnum
CREATE TYPE "UnitType" AS ENUM ('MASS', 'VOLUME', 'COUNT');

-- AlterTable
ALTER TABLE "recipe_ingredient" DROP COLUMN "unit",
ADD COLUMN     "unit_id" INTEGER NOT NULL;

-- CreateTable
CREATE TABLE "unit" (
    "id" SERIAL NOT NULL,
    "key" TEXT NOT NULL,
    "type" "UnitType" NOT NULL,
    "to_base_factor" DECIMAL(12,4) NOT NULL DEFAULT 1,

    CONSTRAINT "unit_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "unit_key_key" ON "unit"("key");

-- AddForeignKey
ALTER TABLE "recipe_ingredient" ADD CONSTRAINT "recipe_ingredient_unit_id_fkey" FOREIGN KEY ("unit_id") REFERENCES "unit"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
