-- CreateTable
CREATE TABLE "Search" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    "title" TEXT,
    "query" TEXT,
    "source" TEXT NOT NULL DEFAULT 'exa',
    "requestId" TEXT,
    "rawResponse" JSONB,
    "latencyMs" INTEGER,
    "error" TEXT,
    "userId" TEXT,
    CONSTRAINT "Search_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "SearchResult" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "searchId" TEXT NOT NULL,
    "rank" INTEGER NOT NULL,
    "title" TEXT,
    "url" TEXT NOT NULL,
    "publishedDate" TEXT,
    "author" TEXT,
    "text" TEXT,
    "highlights" JSONB,
    "highlightScores" JSONB,
    "score" REAL,
    "favicon" TEXT,
    "image" TEXT,
    CONSTRAINT "SearchResult_searchId_fkey" FOREIGN KEY ("searchId") REFERENCES "Search" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateIndex
CREATE INDEX "Search_updatedAt_idx" ON "Search"("updatedAt");

-- CreateIndex
CREATE INDEX "Search_userId_idx" ON "Search"("userId");

-- CreateIndex
CREATE INDEX "SearchResult_searchId_rank_idx" ON "SearchResult"("searchId", "rank");
