-- AlterTable: User
ALTER TABLE "User" ADD COLUMN "cardSizeMode" TEXT NOT NULL DEFAULT 'variable';

-- AlterTable: Note
ALTER TABLE "Note" ADD COLUMN "trashedAt" TIMESTAMP(3);
ALTER TABLE "Note" ALTER COLUMN "checkItems" DROP DEFAULT;
ALTER TABLE "Note" ALTER COLUMN "checkItems" TYPE TEXT USING "checkItems"::TEXT;
ALTER TABLE "Note" ALTER COLUMN "labels" DROP DEFAULT;
ALTER TABLE "Note" ALTER COLUMN "labels" TYPE TEXT USING "labels"::TEXT;
ALTER TABLE "Note" ALTER COLUMN "images" DROP DEFAULT;
ALTER TABLE "Note" ALTER COLUMN "images" TYPE TEXT USING "images"::TEXT;
ALTER TABLE "Note" ALTER COLUMN "links" DROP DEFAULT;
ALTER TABLE "Note" ALTER COLUMN "links" TYPE TEXT USING "links"::TEXT;
ALTER TABLE "Note" ALTER COLUMN "sharedWith" DROP DEFAULT;
ALTER TABLE "Note" ALTER COLUMN "sharedWith" TYPE TEXT USING "sharedWith"::TEXT;
ALTER TABLE "Note" DROP COLUMN "embedding";

-- AlterTable: AiFeedback
ALTER TABLE "AiFeedback" ALTER COLUMN "metadata" DROP DEFAULT;
ALTER TABLE "AiFeedback" ALTER COLUMN "metadata" TYPE TEXT USING "metadata"::TEXT;

-- CreateTable: NoteEmbedding
CREATE TABLE "NoteEmbedding" (
    "id" TEXT NOT NULL,
    "noteId" TEXT NOT NULL,
    "embedding" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE UNIQUE INDEX "NoteEmbedding_noteId_key" ON "NoteEmbedding"("noteId");
CREATE INDEX "NoteEmbedding_noteId_idx" ON "NoteEmbedding"("noteId");

-- CreateTable: Agent
CREATE TABLE "Agent" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "type" TEXT DEFAULT 'scraper',
    "role" TEXT NOT NULL,
    "sourceUrls" TEXT,
    "frequency" TEXT NOT NULL DEFAULT 'manual',
    "lastRun" TIMESTAMP(3),
    "nextRun" TIMESTAMP(3),
    "isEnabled" BOOLEAN NOT NULL DEFAULT true,
    "targetNotebookId" TEXT,
    "sourceNotebookId" TEXT,
    "tools" TEXT DEFAULT '[]',
    "maxSteps" INTEGER NOT NULL DEFAULT 10,
    "notifyEmail" BOOLEAN NOT NULL DEFAULT false,
    "includeImages" BOOLEAN NOT NULL DEFAULT false,
    "userId" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

CREATE INDEX "Agent_userId_idx" ON "Agent"("userId");
CREATE INDEX "Agent_isEnabled_idx" ON "Agent"("isEnabled");

-- CreateTable: AgentAction
CREATE TABLE "AgentAction" (
    "id" TEXT NOT NULL,
    "agentId" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'pending',
    "result" TEXT,
    "log" TEXT,
    "input" TEXT,
    "toolLog" TEXT,
    "tokensUsed" INTEGER,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE INDEX "AgentAction_agentId_idx" ON "AgentAction"("agentId");

-- CreateTable: Conversation
CREATE TABLE "Conversation" (
    "id" TEXT NOT NULL,
    "title" TEXT,
    "userId" TEXT NOT NULL,
    "notebookId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

CREATE INDEX "Conversation_userId_idx" ON "Conversation"("userId");
CREATE INDEX "Conversation_notebookId_idx" ON "Conversation"("notebookId");

-- CreateTable: ChatMessage
CREATE TABLE "ChatMessage" (
    "id" TEXT NOT NULL,
    "conversationId" TEXT NOT NULL,
    "role" TEXT NOT NULL,
    "content" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE INDEX "ChatMessage_conversationId_idx" ON "ChatMessage"("conversationId");

-- CreateTable: Canvas
CREATE TABLE "Canvas" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "data" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

CREATE INDEX "Canvas_userId_idx" ON "Canvas"("userId");

-- CreateTable: Workflow
CREATE TABLE "Workflow" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "graph" TEXT NOT NULL DEFAULT '{"nodes":[],"edges":[]}',
    "isEnabled" BOOLEAN NOT NULL DEFAULT true,
    "userId" TEXT NOT NULL,
    "notebookId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

CREATE INDEX "Workflow_userId_idx" ON "Workflow"("userId");
CREATE INDEX "Workflow_isEnabled_idx" ON "Workflow"("isEnabled");

-- CreateTable: WorkflowRun
CREATE TABLE "WorkflowRun" (
    "id" TEXT NOT NULL,
    "workflowId" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'running',
    "log" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE INDEX "WorkflowRun_workflowId_idx" ON "WorkflowRun"("workflowId");
CREATE INDEX "WorkflowRun_status_idx" ON "WorkflowRun"("status");

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

-- AddForeignKey: Agent
ALTER TABLE "Agent" ADD CONSTRAINT "Agent_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "Agent" ADD CONSTRAINT "Agent_targetNotebookId_fkey" FOREIGN KEY ("targetNotebookId") REFERENCES "Notebook"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey: AgentAction
ALTER TABLE "AgentAction" ADD CONSTRAINT "AgentAction_agentId_fkey" FOREIGN KEY ("agentId") REFERENCES "Agent"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey: Conversation
ALTER TABLE "Conversation" ADD CONSTRAINT "Conversation_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "Conversation" ADD CONSTRAINT "Conversation_notebookId_fkey" FOREIGN KEY ("notebookId") REFERENCES "Notebook"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey: ChatMessage
ALTER TABLE "ChatMessage" ADD CONSTRAINT "ChatMessage_conversationId_fkey" FOREIGN KEY ("conversationId") REFERENCES "Conversation"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey: Canvas
ALTER TABLE "Canvas" ADD CONSTRAINT "Canvas_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey: Workflow
ALTER TABLE "Workflow" ADD CONSTRAINT "Workflow_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "Workflow" ADD CONSTRAINT "Workflow_notebookId_fkey" FOREIGN KEY ("notebookId") REFERENCES "Notebook"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey: WorkflowRun
ALTER TABLE "WorkflowRun" ADD CONSTRAINT "WorkflowRun_workflowId_fkey" FOREIGN KEY ("workflowId") REFERENCES "Workflow"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- Add missing Note index
CREATE INDEX "Note_trashedAt_idx" ON "Note"("trashedAt");
