-- CreateTable
CREATE TABLE "SearchQuery" (
    "id" SERIAL NOT NULL,
    "query" TEXT NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "Product" (
    "id" SERIAL NOT NULL,
    "article" TEXT NOT NULL,
    "title" TEXT,
    "price" DOUBLE PRECISION,
    "imageUrl" TEXT,
    "isCompetitor" BOOLEAN NOT NULL DEFAULT false,
    "searchQueryId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "Position" (
    "id" SERIAL NOT NULL,
    "city" TEXT NOT NULL,
    "position" INTEGER NOT NULL,
    "page" INTEGER NOT NULL,
    "productId" INTEGER NOT NULL,
    "searchQueryId" INTEGER NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "Product_article_key" ON "Product"("article");

-- CreateIndex
CREATE INDEX "Product_article_idx" ON "Product"("article");

-- CreateIndex
CREATE INDEX "Product_searchQueryId_idx" ON "Product"("searchQueryId");

-- CreateIndex
CREATE INDEX "Position_productId_idx" ON "Position"("productId");

-- CreateIndex
CREATE INDEX "Position_searchQueryId_idx" ON "Position"("searchQueryId");

-- CreateIndex
CREATE INDEX "Position_city_idx" ON "Position"("city");

-- AddForeignKey
ALTER TABLE "Product" ADD CONSTRAINT "Product_searchQueryId_fkey" FOREIGN KEY ("searchQueryId") REFERENCES "SearchQuery"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Position" ADD CONSTRAINT "Position_productId_fkey" FOREIGN KEY ("productId") REFERENCES "Product"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Position" ADD CONSTRAINT "Position_searchQueryId_fkey" FOREIGN KEY ("searchQueryId") REFERENCES "SearchQuery"("id") ON DELETE CASCADE ON UPDATE CASCADE;
