-- CreateTable
CREATE TABLE "NotebookSchema" (
    "id" TEXT NOT NULL,
    "notebookId" TEXT NOT NULL,
    "viewSettings" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "NotebookProperty" (
    "id" TEXT NOT NULL,
    "schemaId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "options" TEXT,
    "position" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "NoteProperty" (
    "id" TEXT NOT NULL,
    "noteId" TEXT NOT NULL,
    "propertyId" TEXT NOT NULL,
    "value" TEXT,

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

-- CreateIndex
CREATE UNIQUE INDEX "NotebookSchema_notebookId_key" ON "NotebookSchema"("notebookId");

-- CreateIndex
CREATE INDEX "NotebookProperty_schemaId_position_idx" ON "NotebookProperty"("schemaId", "position");

-- CreateIndex
CREATE INDEX "NoteProperty_noteId_idx" ON "NoteProperty"("noteId");

-- CreateIndex
CREATE INDEX "NoteProperty_propertyId_idx" ON "NoteProperty"("propertyId");

-- CreateIndex
CREATE UNIQUE INDEX "NoteProperty_noteId_propertyId_key" ON "NoteProperty"("noteId", "propertyId");

-- AddForeignKey
ALTER TABLE "NotebookSchema" ADD CONSTRAINT "NotebookSchema_notebookId_fkey" FOREIGN KEY ("notebookId") REFERENCES "Notebook"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "NotebookProperty" ADD CONSTRAINT "NotebookProperty_schemaId_fkey" FOREIGN KEY ("schemaId") REFERENCES "NotebookSchema"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "NoteProperty" ADD CONSTRAINT "NoteProperty_noteId_fkey" FOREIGN KEY ("noteId") REFERENCES "Note"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "NoteProperty" ADD CONSTRAINT "NoteProperty_propertyId_fkey" FOREIGN KEY ("propertyId") REFERENCES "NotebookProperty"("id") ON DELETE CASCADE ON UPDATE CASCADE;
