-- Official platform priorities + nullable creator for community rows (one non-null creatorId still unique)

CREATE TYPE "DemocraticInitiativeOrigin" AS ENUM ('PLATFORM', 'COMMUNITY');

ALTER TABLE "DemocraticInitiative" DROP CONSTRAINT IF EXISTS "DemocraticInitiative_creatorId_key";

ALTER TABLE "DemocraticInitiative" ADD COLUMN "origin" "DemocraticInitiativeOrigin" NOT NULL DEFAULT 'COMMUNITY';
ALTER TABLE "DemocraticInitiative" ADD COLUMN "sortOrder" INTEGER NOT NULL DEFAULT 0;

ALTER TABLE "DemocraticInitiative" ALTER COLUMN "creatorId" DROP NOT NULL;

CREATE UNIQUE INDEX "DemocraticInitiative_creatorId_key" ON "DemocraticInitiative"("creatorId");

CREATE INDEX "DemocraticInitiative_origin_sortOrder_idx" ON "DemocraticInitiative"("origin", "sortOrder");
CREATE INDEX "DemocraticInitiative_origin_createdAt_idx" ON "DemocraticInitiative"("origin", "createdAt");
