-- Migration: Task-First Workflow Refactor
-- 1. Delete old shot-based versions (versions linked to shots, not tasks)
DELETE FROM "versions" WHERE "shotId" IS NOT NULL AND "taskId" IS NULL;

-- 2. Drop defaults before type changes (they hold a reference to the enum)
ALTER TABLE "shots" ALTER COLUMN "status" DROP DEFAULT;
ALTER TABLE "assets" ALTER COLUMN "status" DROP DEFAULT;

-- 3. Cast status columns to text so we can rename values
ALTER TABLE "shots" ALTER COLUMN "status" TYPE TEXT;
ALTER TABLE "assets" ALTER COLUMN "status" TYPE TEXT;

-- 4. Rename old enum values in the data
UPDATE "shots" SET "status" = 'IN_REVIEW' WHERE "status" IN ('INTERNAL_REVIEW', 'CLIENT_REVIEW');
UPDATE "shots" SET "status" = 'COMPLETE' WHERE "status" IN ('APPROVED', 'FINAL');
UPDATE "assets" SET "status" = 'IN_REVIEW' WHERE "status" IN ('INTERNAL_REVIEW', 'CLIENT_REVIEW');
UPDATE "assets" SET "status" = 'COMPLETE' WHERE "status" IN ('APPROVED', 'FINAL');

-- 5. Drop old enum type (no dependents remain)
DROP TYPE "ShotStatus";

-- 6. Create new enum type with 5 values
CREATE TYPE "ShotStatus" AS ENUM ('WAITING', 'IN_PROGRESS', 'IN_REVIEW', 'REVISIONS', 'COMPLETE');

-- 7. Convert columns back to the new enum and restore defaults
ALTER TABLE "shots" ALTER COLUMN "status" TYPE "ShotStatus" USING "status"::"ShotStatus";
ALTER TABLE "shots" ALTER COLUMN "status" SET DEFAULT 'WAITING';
ALTER TABLE "assets" ALTER COLUMN "status" TYPE "ShotStatus" USING "status"::"ShotStatus";
ALTER TABLE "assets" ALTER COLUMN "status" SET DEFAULT 'WAITING';
