-- Step 1: Add new columns as nullable (preserva las 4 filas existentes con sus precios)
ALTER TABLE "despacho_plan_prices"
  ADD COLUMN "nombre" VARCHAR(50),
  ADD COLUMN "max_rfcs" INTEGER,
  ADD COLUMN "max_users" INTEGER,
  ADD COLUMN "db_mode" "DbMode",
  ADD COLUMN "timbres_incluidos_mes" INTEGER NOT NULL DEFAULT 0,
  ADD COLUMN "permite_servidor_backup" BOOLEAN NOT NULL DEFAULT false;

-- Step 2: Backfill limits para las 4 filas existentes desde el catálogo TS
UPDATE "despacho_plan_prices" SET
  "nombre" = 'Mi Empresa',
  "max_rfcs" = 1,
  "max_users" = 3,
  "timbres_incluidos_mes" = 50,
  "db_mode" = 'MANAGED'
WHERE "plan" = 'mi_empresa';

UPDATE "despacho_plan_prices" SET
  "nombre" = 'Mi Empresa +',
  "max_rfcs" = 1,
  "max_users" = 3,
  "timbres_incluidos_mes" = 50,
  "db_mode" = 'MANAGED'
WHERE "plan" = 'mi_empresa_plus';

UPDATE "despacho_plan_prices" SET
  "nombre" = 'Business Control',
  "max_rfcs" = 100,
  "max_users" = -1,
  "timbres_incluidos_mes" = 0,
  "db_mode" = 'BYO',
  "permite_servidor_backup" = true
WHERE "plan" = 'business_control';

UPDATE "despacho_plan_prices" SET
  "nombre" = 'Enterprise',
  "max_rfcs" = 100,
  "max_users" = -1,
  "timbres_incluidos_mes" = 0,
  "db_mode" = 'BYO',
  "permite_servidor_backup" = true
WHERE "plan" = 'business_cloud';

-- Step 3: Set NOT NULL después del backfill (las 4 filas ya están completas)
ALTER TABLE "despacho_plan_prices"
  ALTER COLUMN "nombre" SET NOT NULL,
  ALTER COLUMN "max_rfcs" SET NOT NULL,
  ALTER COLUMN "max_users" SET NOT NULL,
  ALTER COLUMN "db_mode" SET NOT NULL;

-- Step 4: Hacer firstYear y renewal nullable para soportar trial y custom (sin precio fijo)
ALTER TABLE "despacho_plan_prices"
  ALTER COLUMN "first_year" DROP NOT NULL,
  ALTER COLUMN "renewal" DROP NOT NULL;
