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

-- CreateTable
CREATE TABLE "binom_v2_account" (
    "id" SERIAL NOT NULL,
    "apiKey" TEXT NOT NULL,
    "costTimezone" TEXT NOT NULL,
    "trackerUrl" TEXT NOT NULL,
    "trackerAccountId" INTEGER NOT NULL,
    "costUpdateBinomV2TrafficSourceTokenId" INTEGER
);

-- CreateTable
CREATE TABLE "binom_v2_campaign" (
    "id" SERIAL NOT NULL,
    "externalId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "isDomainBanned" BOOLEAN NOT NULL,
    "binomV2AccountId" INTEGER NOT NULL,
    "binomV2CampaignGroupId" INTEGER NOT NULL,
    "binomV2TrafficSourceId" INTEGER NOT NULL
);

-- CreateTable
CREATE TABLE "binom_v2_campaign_group" (
    "id" SERIAL NOT NULL,
    "externalId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "binomV2AccountId" INTEGER NOT NULL
);

-- CreateTable
CREATE TABLE "binom_v2_traffic_source" (
    "id" SERIAL NOT NULL,
    "externalId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "binomV2AccountId" INTEGER NOT NULL
);

-- CreateTable
CREATE TABLE "binom_v2_traffic_source_token" (
    "id" SERIAL NOT NULL,
    "externalId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "parameter" TEXT NOT NULL,
    "placeholder" TEXT NOT NULL,
    "binomV2TrafficSourceId" INTEGER NOT NULL
);

-- CreateTable
CREATE TABLE "binom_v2_campaign_date_stat" (
    "id" SERIAL NOT NULL,
    "clicks" INTEGER NOT NULL,
    "leads" INTEGER NOT NULL,
    "cost" DOUBLE PRECISION NOT NULL,
    "revenue" DOUBLE PRECISION NOT NULL,
    "tokenName" TEXT NOT NULL,
    "statDate" TIMESTAMP(3) NOT NULL,
    "binomV2CampaignId" INTEGER NOT NULL,
    "binomV2TrafficSourceTokenId" INTEGER
);

-- CreateTable
CREATE TABLE "tracker_account" (
    "id" SERIAL NOT NULL,
    "createAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateAt" TIMESTAMP(3) NOT NULL,
    "trackerType" "TrackerType" NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "binom_v2_account_id_key" ON "binom_v2_account"("id");

-- CreateIndex
CREATE UNIQUE INDEX "binom_v2_account_trackerAccountId_key" ON "binom_v2_account"("trackerAccountId");

-- CreateIndex
CREATE UNIQUE INDEX "binom_v2_account_costUpdateBinomV2TrafficSourceTokenId_key" ON "binom_v2_account"("costUpdateBinomV2TrafficSourceTokenId");

-- CreateIndex
CREATE UNIQUE INDEX "binom_v2_campaign_id_key" ON "binom_v2_campaign"("id");

-- CreateIndex
CREATE UNIQUE INDEX "binom_v2_campaign_group_id_key" ON "binom_v2_campaign_group"("id");

-- CreateIndex
CREATE UNIQUE INDEX "binom_v2_traffic_source_id_key" ON "binom_v2_traffic_source"("id");

-- CreateIndex
CREATE UNIQUE INDEX "binom_v2_traffic_source_token_id_key" ON "binom_v2_traffic_source_token"("id");

-- CreateIndex
CREATE UNIQUE INDEX "binom_v2_campaign_date_stat_id_key" ON "binom_v2_campaign_date_stat"("id");

-- CreateIndex
CREATE UNIQUE INDEX "tracker_account_id_key" ON "tracker_account"("id");

-- AddForeignKey
ALTER TABLE "binom_v2_account" ADD CONSTRAINT "binom_v2_account_trackerAccountId_fkey" FOREIGN KEY ("trackerAccountId") REFERENCES "tracker_account"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "binom_v2_account" ADD CONSTRAINT "binom_v2_account_costUpdateBinomV2TrafficSourceTokenId_fkey" FOREIGN KEY ("costUpdateBinomV2TrafficSourceTokenId") REFERENCES "binom_v2_traffic_source_token"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "binom_v2_campaign" ADD CONSTRAINT "binom_v2_campaign_binomV2AccountId_fkey" FOREIGN KEY ("binomV2AccountId") REFERENCES "binom_v2_account"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "binom_v2_campaign" ADD CONSTRAINT "binom_v2_campaign_binomV2CampaignGroupId_fkey" FOREIGN KEY ("binomV2CampaignGroupId") REFERENCES "binom_v2_campaign_group"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "binom_v2_campaign" ADD CONSTRAINT "binom_v2_campaign_binomV2TrafficSourceId_fkey" FOREIGN KEY ("binomV2TrafficSourceId") REFERENCES "binom_v2_traffic_source"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "binom_v2_campaign_group" ADD CONSTRAINT "binom_v2_campaign_group_binomV2AccountId_fkey" FOREIGN KEY ("binomV2AccountId") REFERENCES "binom_v2_account"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "binom_v2_traffic_source" ADD CONSTRAINT "binom_v2_traffic_source_binomV2AccountId_fkey" FOREIGN KEY ("binomV2AccountId") REFERENCES "binom_v2_account"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "binom_v2_traffic_source_token" ADD CONSTRAINT "binom_v2_traffic_source_token_binomV2TrafficSourceId_fkey" FOREIGN KEY ("binomV2TrafficSourceId") REFERENCES "binom_v2_traffic_source"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "binom_v2_campaign_date_stat" ADD CONSTRAINT "binom_v2_campaign_date_stat_binomV2CampaignId_fkey" FOREIGN KEY ("binomV2CampaignId") REFERENCES "binom_v2_campaign"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "binom_v2_campaign_date_stat" ADD CONSTRAINT "binom_v2_campaign_date_stat_binomV2TrafficSourceTokenId_fkey" FOREIGN KEY ("binomV2TrafficSourceTokenId") REFERENCES "binom_v2_traffic_source_token"("id") ON DELETE SET NULL ON UPDATE CASCADE;
