-- CreateTable
CREATE TABLE "actuate_folders" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "scope" TEXT NOT NULL,
    "parentId" TEXT,
    "position" INTEGER NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "actuate_folders_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE INDEX "actuate_folders_scope_idx" ON "actuate_folders"("scope");

-- CreateIndex
CREATE INDEX "actuate_folders_parentId_idx" ON "actuate_folders"("parentId");

-- AddForeignKey (self-relation for nesting)
ALTER TABLE "actuate_folders" ADD CONSTRAINT "actuate_folders_parentId_fkey" FOREIGN KEY ("parentId") REFERENCES "actuate_folders"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AlterTable: add folderId to documents
ALTER TABLE "actuate_documents" ADD COLUMN "folderId" TEXT;

-- CreateIndex
CREATE INDEX "actuate_documents_folderId_idx" ON "actuate_documents"("folderId");

-- AddForeignKey
ALTER TABLE "actuate_documents" ADD CONSTRAINT "actuate_documents_folderId_fkey" FOREIGN KEY ("folderId") REFERENCES "actuate_folders"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AlterTable: add folderId to media
ALTER TABLE "actuate_media" ADD COLUMN "folderId" TEXT;

-- CreateIndex
CREATE INDEX "actuate_media_folderId_idx" ON "actuate_media"("folderId");

-- AddForeignKey
ALTER TABLE "actuate_media" ADD CONSTRAINT "actuate_media_folderId_fkey" FOREIGN KEY ("folderId") REFERENCES "actuate_folders"("id") ON DELETE SET NULL ON UPDATE CASCADE;
