-- Drops AWARD_MASTER from the UserRole enum.
--
-- Any row still holding AWARD_MASTER is demoted to JURY_MEMBER (singular role)
-- or filtered out of the roles[] array (multi-role) before the enum swap, so
-- the type alteration is safe even if the prod migration was missed.

UPDATE "User" SET role = 'JURY_MEMBER' WHERE role = 'AWARD_MASTER';
UPDATE "User" SET roles = array_remove(roles, 'AWARD_MASTER') WHERE 'AWARD_MASTER' = ANY(roles);

CREATE TYPE "UserRole_new" AS ENUM (
    'SUPER_ADMIN',
    'PROGRAM_ADMIN',
    'JURY_MEMBER',
    'MENTOR',
    'OBSERVER',
    'APPLICANT',
    'AUDIENCE'
);

ALTER TABLE "User" ALTER COLUMN role DROP DEFAULT;
ALTER TABLE "User"
    ALTER COLUMN role TYPE "UserRole_new" USING role::text::"UserRole_new";
ALTER TABLE "User" ALTER COLUMN role SET DEFAULT 'APPLICANT';

ALTER TABLE "User"
    ALTER COLUMN roles TYPE "UserRole_new"[] USING roles::text[]::"UserRole_new"[];

DROP TYPE "UserRole";
ALTER TYPE "UserRole_new" RENAME TO "UserRole";
