-- CreateEnum
CREATE TYPE "DomainDestination" AS ENUM ('BINOM_V1', 'BINOM_V2', 'ADSCOM');

-- AlterEnum
-- This migration adds more than one value to an enum.
-- With PostgreSQL versions 11 and earlier, this is not possible
-- in a single migration. This can be worked around by creating
-- multiple migrations, each migration adding only one value to
-- the enum.


ALTER TYPE "RequestType" ADD VALUE 'NEW_BUYER_ACCESS';
ALTER TYPE "RequestType" ADD VALUE 'REMOVE_BUYER_ACCESS';
ALTER TYPE "RequestType" ADD VALUE 'COST_TROUBLE';
ALTER TYPE "RequestType" ADD VALUE 'REVENUE_TROUBLE';
ALTER TYPE "RequestType" ADD VALUE 'ADD_DOMAIN';
ALTER TYPE "RequestType" ADD VALUE 'OTHER';

-- CreateTable
CREATE TABLE "other_request" (
    "id" SERIAL NOT NULL,
    "notes" TEXT NOT NULL,
    "requestId" INTEGER NOT NULL
);

-- CreateTable
CREATE TABLE "new_buyer_access_request" (
    "id" SERIAL NOT NULL,
    "requestId" INTEGER NOT NULL
);

-- CreateTable
CREATE TABLE "new_buyer_access_request_entity" (
    "id" SERIAL NOT NULL,
    "userName" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "telegramTag" TEXT NOT NULL,
    "teamId" INTEGER NOT NULL,
    "newBuyerAccessRequestId" INTEGER NOT NULL,
    "notes" TEXT NOT NULL
);

-- CreateTable
CREATE TABLE "remove_buyer_access_request" (
    "id" SERIAL NOT NULL,
    "requestId" INTEGER NOT NULL
);

-- CreateTable
CREATE TABLE "remove_buyer_access_request_entity" (
    "id" SERIAL NOT NULL,
    "userId" INTEGER NOT NULL,
    "removeBuyerAccessRequestId" INTEGER NOT NULL
);

-- CreateTable
CREATE TABLE "cost_trouble_request" (
    "id" SERIAL NOT NULL,
    "requestId" INTEGER NOT NULL
);

-- CreateTable
CREATE TABLE "cost_trouble_request_entity" (
    "id" SERIAL NOT NULL,
    "trackerCampaignExternalIds" TEXT[],
    "trafficSource" "TrafficSource" NOT NULL,
    "troubleDateFrom" TIMESTAMP(3) NOT NULL,
    "troubleDateTo" TIMESTAMP(3) NOT NULL,
    "notes" TEXT NOT NULL,
    "accountId" INTEGER NOT NULL,
    "costTroubleRequestId" INTEGER NOT NULL
);

-- CreateTable
CREATE TABLE "revenue_trouble_request" (
    "id" SERIAL NOT NULL,
    "requestId" INTEGER NOT NULL
);

-- CreateTable
CREATE TABLE "revenue_trouble_request_entity" (
    "id" SERIAL NOT NULL,
    "trackerCampaignExternalIds" TEXT[],
    "provider" "Provider" NOT NULL,
    "troubleDateFrom" TIMESTAMP(3) NOT NULL,
    "troubleDateTo" TIMESTAMP(3) NOT NULL,
    "notes" TEXT NOT NULL,
    "revenueTroubleRequestId" INTEGER NOT NULL
);

-- CreateTable
CREATE TABLE "add_domain_request" (
    "id" SERIAL NOT NULL,
    "requestId" INTEGER NOT NULL
);

-- CreateTable
CREATE TABLE "add_domain_request_entity" (
    "id" SERIAL NOT NULL,
    "domainNames" TEXT[],
    "domainsDestination" "DomainDestination" NOT NULL,
    "notes" TEXT NOT NULL,
    "addDomainRequestId" INTEGER NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "other_request_id_key" ON "other_request"("id");

-- CreateIndex
CREATE UNIQUE INDEX "other_request_requestId_key" ON "other_request"("requestId");

-- CreateIndex
CREATE UNIQUE INDEX "new_buyer_access_request_id_key" ON "new_buyer_access_request"("id");

-- CreateIndex
CREATE UNIQUE INDEX "new_buyer_access_request_requestId_key" ON "new_buyer_access_request"("requestId");

-- CreateIndex
CREATE UNIQUE INDEX "new_buyer_access_request_entity_id_key" ON "new_buyer_access_request_entity"("id");

-- CreateIndex
CREATE UNIQUE INDEX "remove_buyer_access_request_id_key" ON "remove_buyer_access_request"("id");

-- CreateIndex
CREATE UNIQUE INDEX "remove_buyer_access_request_requestId_key" ON "remove_buyer_access_request"("requestId");

-- CreateIndex
CREATE UNIQUE INDEX "remove_buyer_access_request_entity_id_key" ON "remove_buyer_access_request_entity"("id");

-- CreateIndex
CREATE UNIQUE INDEX "cost_trouble_request_id_key" ON "cost_trouble_request"("id");

-- CreateIndex
CREATE UNIQUE INDEX "cost_trouble_request_requestId_key" ON "cost_trouble_request"("requestId");

-- CreateIndex
CREATE UNIQUE INDEX "cost_trouble_request_entity_id_key" ON "cost_trouble_request_entity"("id");

-- CreateIndex
CREATE UNIQUE INDEX "revenue_trouble_request_id_key" ON "revenue_trouble_request"("id");

-- CreateIndex
CREATE UNIQUE INDEX "revenue_trouble_request_requestId_key" ON "revenue_trouble_request"("requestId");

-- CreateIndex
CREATE UNIQUE INDEX "revenue_trouble_request_entity_id_key" ON "revenue_trouble_request_entity"("id");

-- CreateIndex
CREATE UNIQUE INDEX "add_domain_request_id_key" ON "add_domain_request"("id");

-- CreateIndex
CREATE UNIQUE INDEX "add_domain_request_requestId_key" ON "add_domain_request"("requestId");

-- CreateIndex
CREATE UNIQUE INDEX "add_domain_request_entity_id_key" ON "add_domain_request_entity"("id");

-- AddForeignKey
ALTER TABLE "other_request" ADD CONSTRAINT "other_request_requestId_fkey" FOREIGN KEY ("requestId") REFERENCES "request"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "new_buyer_access_request" ADD CONSTRAINT "new_buyer_access_request_requestId_fkey" FOREIGN KEY ("requestId") REFERENCES "request"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "new_buyer_access_request_entity" ADD CONSTRAINT "new_buyer_access_request_entity_teamId_fkey" FOREIGN KEY ("teamId") REFERENCES "team"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "new_buyer_access_request_entity" ADD CONSTRAINT "new_buyer_access_request_entity_newBuyerAccessRequestId_fkey" FOREIGN KEY ("newBuyerAccessRequestId") REFERENCES "new_buyer_access_request"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "remove_buyer_access_request" ADD CONSTRAINT "remove_buyer_access_request_requestId_fkey" FOREIGN KEY ("requestId") REFERENCES "request"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "remove_buyer_access_request_entity" ADD CONSTRAINT "remove_buyer_access_request_entity_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "remove_buyer_access_request_entity" ADD CONSTRAINT "remove_buyer_access_request_entity_removeBuyerAccessReques_fkey" FOREIGN KEY ("removeBuyerAccessRequestId") REFERENCES "remove_buyer_access_request"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "cost_trouble_request" ADD CONSTRAINT "cost_trouble_request_requestId_fkey" FOREIGN KEY ("requestId") REFERENCES "request"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "cost_trouble_request_entity" ADD CONSTRAINT "cost_trouble_request_entity_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "account"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "cost_trouble_request_entity" ADD CONSTRAINT "cost_trouble_request_entity_costTroubleRequestId_fkey" FOREIGN KEY ("costTroubleRequestId") REFERENCES "cost_trouble_request"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "revenue_trouble_request" ADD CONSTRAINT "revenue_trouble_request_requestId_fkey" FOREIGN KEY ("requestId") REFERENCES "request"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "revenue_trouble_request_entity" ADD CONSTRAINT "revenue_trouble_request_entity_revenueTroubleRequestId_fkey" FOREIGN KEY ("revenueTroubleRequestId") REFERENCES "revenue_trouble_request"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "add_domain_request" ADD CONSTRAINT "add_domain_request_requestId_fkey" FOREIGN KEY ("requestId") REFERENCES "request"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "add_domain_request_entity" ADD CONSTRAINT "add_domain_request_entity_addDomainRequestId_fkey" FOREIGN KEY ("addDomainRequestId") REFERENCES "add_domain_request"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
