-- CreateTable
CREATE TABLE "NoteEmbeddingChunk" (
    "id" TEXT NOT NULL,
    "noteId" TEXT NOT NULL,
    "fragmentId" TEXT NOT NULL,
    "chunkIndex" INTEGER NOT NULL,
    "content" TEXT NOT NULL,
    "charCount" INTEGER NOT NULL,
    "embedding" vector(1536),
    "embeddingModel" TEXT DEFAULT 'text-embedding-3-small',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "NoteEmbeddingChunk_noteId_fragmentId_key" ON "NoteEmbeddingChunk"("noteId", "fragmentId");

-- CreateIndex
CREATE INDEX "NoteEmbeddingChunk_noteId_idx" ON "NoteEmbeddingChunk"("noteId");

-- CreateIndex
CREATE INDEX "NoteEmbeddingChunk_fragmentId_idx" ON "NoteEmbeddingChunk"("fragmentId");

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

-- CreateIndex: HNSW index for fast cosine similarity search on chunk embeddings
CREATE INDEX "NoteEmbeddingChunk_embedding_hnsw_idx"
    ON "NoteEmbeddingChunk" USING hnsw ("embedding" vector_cosine_ops)
    WITH (m = 16, ef_construction = 64);
