CREATE TYPE "AccountType" AS ENUM ('APP', 'PANEL');

ALTER TABLE "identity_users"
  ADD COLUMN "account_type" "AccountType" NOT NULL DEFAULT 'APP';

UPDATE "identity_users"
SET "account_type" = 'PANEL'
WHERE cardinality("roles") > 0;

UPDATE "identity_users"
SET "permissions" = array_append("permissions", 'users:manage')
WHERE "roles" @> ARRAY['super_admin']::TEXT[]
  AND NOT ("permissions" @> ARRAY['users:manage']::TEXT[]);

CREATE INDEX "identity_users_account_type_created_at_idx"
  ON "identity_users"("account_type", "created_at");
