-- Subscriptions: add package_id (nullable first for backfill), add seats, drop wm_id
ALTER TABLE "subscriptions" ADD COLUMN "package_id" uuid;--> statement-breakpoint
ALTER TABLE "subscriptions" ADD COLUMN "seats" integer DEFAULT 1 NOT NULL;--> statement-breakpoint

-- Backfill package_id from wms via wm_id join
UPDATE "subscriptions" s
SET "package_id" = w."package_id"
FROM "wms" w
WHERE s."wm_id" = w."id" AND w."package_id" IS NOT NULL;--> statement-breakpoint

-- Make package_id NOT NULL and add FK
ALTER TABLE "subscriptions" ALTER COLUMN "package_id" SET NOT NULL;--> statement-breakpoint
ALTER TABLE "subscriptions" ADD CONSTRAINT "subscriptions_package_id_packages_id_fk" FOREIGN KEY ("package_id") REFERENCES "public"."packages"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint

-- Drop wm_id FK and column from subscriptions
ALTER TABLE "subscriptions" DROP CONSTRAINT IF EXISTS "subscriptions_wm_id_wms_id_fk";--> statement-breakpoint
ALTER TABLE "subscriptions" DROP COLUMN IF EXISTS "wm_id";--> statement-breakpoint

-- Checkouts: drop columns no longer needed
ALTER TABLE "checkouts" DROP CONSTRAINT IF EXISTS "checkouts_project_id_projects_id_fk";--> statement-breakpoint
ALTER TABLE "checkouts" DROP CONSTRAINT IF EXISTS "checkouts_wm_id_wms_id_fk";--> statement-breakpoint
ALTER TABLE "checkouts" DROP COLUMN IF EXISTS "project_id";--> statement-breakpoint
ALTER TABLE "checkouts" DROP COLUMN IF EXISTS "wm_name";--> statement-breakpoint
ALTER TABLE "checkouts" DROP COLUMN IF EXISTS "vcpu";--> statement-breakpoint
ALTER TABLE "checkouts" DROP COLUMN IF EXISTS "ram";--> statement-breakpoint
ALTER TABLE "checkouts" DROP COLUMN IF EXISTS "disk";--> statement-breakpoint
ALTER TABLE "checkouts" DROP COLUMN IF EXISTS "wm_id";
