-- Idempotent SQL applied BEFORE `prisma db push`.
-- Flips any residual SERVICE-role users to AGENT before Prisma rewrites the Role enum.
-- Safe no-op on fresh databases or databases already migrated past the SERVICE role.

DO $$
BEGIN
  IF EXISTS (
    SELECT 1 FROM pg_type t
    JOIN pg_enum e ON e.enumtypid = t.oid
    WHERE t.typname = 'Role' AND e.enumlabel = 'SERVICE'
  ) THEN
    EXECUTE 'UPDATE "User" SET "role" = ''AGENT'' WHERE "role"::text = ''SERVICE''';
  END IF;
END $$;
