-- CreateEnum
CREATE TYPE "PixelType" AS ENUM ('TIKTOK', 'FACEBOOK');

-- CreateEnum
CREATE TYPE "WorkerType" AS ENUM ('INTERVAL', 'SPECIFIC_TIME');

-- CreateEnum
CREATE TYPE "Role" AS ENUM ('OWNER', 'ADMIN', 'TEAMLEAD', 'BUYER');

-- CreateEnum
CREATE TYPE "TrafficSource" AS ENUM ('FACEBOOK', 'TIKTOK');

-- CreateEnum
CREATE TYPE "TonicAccountType" AS ENUM ('FACEBOOK', 'TIKTOK', 'TOBOOLA');

-- CreateEnum
CREATE TYPE "UserStatus" AS ENUM ('FIRED', 'EMPLOYED');

-- CreateEnum
CREATE TYPE "BusinessDirection" AS ENUM ('SEARCH', 'ECOMMERCE', 'NONE');

-- CreateEnum
CREATE TYPE "AccountOwnershipChangeAction" AS ENUM ('ADD', 'REMOVE');

-- CreateEnum
CREATE TYPE "ConvertionStatus" AS ENUM ('lead', 'final_lead');

-- CreateTable
CREATE TABLE "user" (
    "id" SERIAL NOT NULL,
    "createAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateAt" TIMESTAMP(3) NOT NULL,
    "email" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "password" TEXT NOT NULL,
    "role" "Role" NOT NULL,
    "note" TEXT,
    "status" "UserStatus" NOT NULL,
    "archived" BOOLEAN NOT NULL DEFAULT false,
    "referalLine" INTEGER NOT NULL DEFAULT 0,
    "teamId" INTEGER,
    "binomUserId" INTEGER,
    "telegramUserId" INTEGER,
    "referalOwnerId" INTEGER,
    "billingAccountId" INTEGER,

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

-- CreateTable
CREATE TABLE "billing_account" (
    "id" SERIAL NOT NULL,
    "createAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateAt" TIMESTAMP(3) NOT NULL,
    "userRevenuePercent" DOUBLE PRECISION NOT NULL,
    "teamRevenuePercent" DOUBLE PRECISION,

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

-- CreateTable
CREATE TABLE "tiktok_campaign" (
    "id" SERIAL NOT NULL,
    "createAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateAt" TIMESTAMP(3) NOT NULL,
    "operationStatus" TEXT NOT NULL,
    "createTikTokTime" TIMESTAMP(3) NOT NULL,
    "campaignName" TEXT NOT NULL,
    "tikTokCampaignId" TEXT NOT NULL,
    "tikTokAccountId" TEXT NOT NULL,
    "budget" DOUBLE PRECISION NOT NULL,
    "accountId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "tiktok_adgroup" (
    "id" SERIAL NOT NULL,
    "createAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateAt" TIMESTAMP(3) NOT NULL,
    "operationStatus" TEXT NOT NULL,
    "createTikTokTime" TIMESTAMP(3) NOT NULL,
    "adgroupName" TEXT NOT NULL,
    "tikTokCampaignId" TEXT NOT NULL,
    "tikTokAdgroupId" TEXT NOT NULL,
    "tikTokAccountId" TEXT NOT NULL,
    "budget" DOUBLE PRECISION NOT NULL,
    "pixelId" TEXT,
    "campaignId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "tiktok_ad" (
    "id" SERIAL NOT NULL,
    "createAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateAt" TIMESTAMP(3) NOT NULL,
    "operationStatus" TEXT NOT NULL,
    "createTikTokTime" TIMESTAMP(3) NOT NULL,
    "adName" TEXT NOT NULL,
    "tikTokAdId" TEXT NOT NULL,
    "tikTokAdgroupId" TEXT NOT NULL,
    "tikTokCampaignId" TEXT NOT NULL,
    "tikTokAccountId" TEXT NOT NULL,
    "trackingPixelId" TEXT,
    "landingPageUrl" TEXT NOT NULL,
    "adgroupId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "tiktok_ad_stat" (
    "id" SERIAL NOT NULL,
    "date" TIMESTAMP(3) NOT NULL,
    "spend" DOUBLE PRECISION NOT NULL,
    "conversions" INTEGER NOT NULL,
    "impressions" INTEGER NOT NULL,
    "clicks" INTEGER NOT NULL,
    "tikTokAdId" TEXT NOT NULL,
    "tikTokAdgroupId" TEXT NOT NULL,
    "tikTokCampaignId" TEXT NOT NULL,
    "tikTokAccountId" TEXT NOT NULL,
    "adId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "facebook_campaign" (
    "id" SERIAL NOT NULL,
    "createAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateAt" TIMESTAMP(3) NOT NULL,
    "status" TEXT NOT NULL,
    "createFbCampaignDate" TIMESTAMP(3) NOT NULL,
    "facebookCampaignId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "facebookAccountId" TEXT NOT NULL,
    "dailyBudget" DOUBLE PRECISION,
    "budgetRemaining" DOUBLE PRECISION NOT NULL,
    "startTime" TIMESTAMP(3),
    "stopTime" TIMESTAMP(3),
    "accountId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "facebook_adgroup" (
    "id" SERIAL NOT NULL,
    "createAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateAt" TIMESTAMP(3) NOT NULL,
    "status" TEXT NOT NULL,
    "createFbAdgroupDate" TIMESTAMP(3) NOT NULL,
    "facebookCampaignId" TEXT NOT NULL,
    "facebookAccountId" TEXT NOT NULL,
    "facebookAdgroupId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "dailyBudget" DOUBLE PRECISION,
    "budgetRemaining" DOUBLE PRECISION NOT NULL,
    "campaignId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "facebook_ad" (
    "id" SERIAL NOT NULL,
    "createAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateAt" TIMESTAMP(3) NOT NULL,
    "status" TEXT NOT NULL,
    "createFbAdgroupDate" TIMESTAMP(3) NOT NULL,
    "facebookCampaignId" TEXT NOT NULL,
    "facebookAccountId" TEXT NOT NULL,
    "facebookAdgroupId" TEXT NOT NULL,
    "facebookAdId" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "landingPageUrl" TEXT,
    "adgroupId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "binom_user" (
    "id" SERIAL NOT NULL,
    "createAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateAt" TIMESTAMP(3) NOT NULL,
    "name" TEXT NOT NULL,
    "groupOffId" TEXT NOT NULL,
    "groupCampId" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "tonic_campaign" (
    "id" SERIAL NOT NULL,
    "createAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateAt" TIMESTAMP(3) NOT NULL,
    "tonicCampaignId" INTEGER,
    "tonicCampaignName" TEXT NOT NULL,
    "keywords" TEXT NOT NULL DEFAULT '[]',
    "pixelId" INTEGER,

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

-- CreateTable
CREATE TABLE "account_fee" (
    "id" SERIAL NOT NULL,
    "startDate" TIMESTAMP(3) NOT NULL,
    "endDate" TIMESTAMP(3),
    "accountId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "account_ownership_change" (
    "id" SERIAL NOT NULL,
    "date" TIMESTAMP(3) NOT NULL,
    "accountBalance" DOUBLE PRECISION NOT NULL,
    "action" "AccountOwnershipChangeAction" NOT NULL,
    "accountId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "account" (
    "id" SERIAL NOT NULL,
    "createAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateAt" TIMESTAMP(3) NOT NULL,
    "accountBlockDate" TIMESTAMP(3),
    "note" TEXT,
    "accountName" TEXT NOT NULL,
    "accountCreateTime" TIMESTAMP(3) NOT NULL,
    "accountTimezone" TEXT NOT NULL,
    "type" "TrafficSource" NOT NULL,
    "accountId" TEXT NOT NULL,
    "currency" TEXT NOT NULL,
    "balance" INTEGER NOT NULL,
    "status" TEXT NOT NULL,
    "archived" BOOLEAN NOT NULL DEFAULT false,
    "businessDirection" "BusinessDirection",
    "active" BOOLEAN NOT NULL DEFAULT true,
    "agencyId" INTEGER,
    "businessManagerId" INTEGER,

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

-- CreateTable
CREATE TABLE "system_settings" (
    "id" SERIAL NOT NULL,
    "binomTimezone" TEXT NOT NULL,
    "binomSsnUrl" TEXT NOT NULL,
    "binomArmUrl" TEXT NOT NULL,
    "binomDomain" TEXT NOT NULL,
    "binomApiKey" TEXT NOT NULL,
    "tonicFinalizationHour" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "business_manager" (
    "id" SERIAL NOT NULL,
    "createAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateAt" TIMESTAMP(3) NOT NULL,
    "name" TEXT NOT NULL,
    "bmId" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "account_to_user" (
    "accountId" INTEGER NOT NULL,
    "userId" INTEGER NOT NULL,

    CONSTRAINT "account_to_user_pkey" PRIMARY KEY ("accountId","userId")
);

-- CreateTable
CREATE TABLE "agency" (
    "id" SERIAL NOT NULL,
    "createAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateAt" TIMESTAMP(3) NOT NULL,
    "name" TEXT NOT NULL,
    "note" TEXT,
    "color" TEXT,
    "archived" BOOLEAN NOT NULL DEFAULT false,

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

-- CreateTable
CREATE TABLE "team" (
    "id" SERIAL NOT NULL,
    "createAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateAt" TIMESTAMP(3) NOT NULL,
    "name" TEXT NOT NULL,
    "note" TEXT,
    "binomName" TEXT,
    "color" TEXT,
    "archived" BOOLEAN NOT NULL DEFAULT false,
    "teamRevenuePercent" DOUBLE PRECISION NOT NULL DEFAULT 0,

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

-- CreateTable
CREATE TABLE "lead_to_send_to_tracker" (
    "id" SERIAL NOT NULL,
    "trackerId" TEXT NOT NULL,
    "revenue" DOUBLE PRECISION NOT NULL,
    "event1" TEXT,
    "event2" TEXT,
    "leadDate" TIMESTAMP(3) NOT NULL,
    "conversionStatus" "ConvertionStatus" NOT NULL,

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

-- CreateTable
CREATE TABLE "tonic_lead" (
    "id" SERIAL NOT NULL,
    "createAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateAt" TIMESTAMP(3) NOT NULL,
    "clicks" INTEGER NOT NULL,
    "revenue" DOUBLE PRECISION NOT NULL,
    "trackerClickId" TEXT,
    "keyword" TEXT NOT NULL,
    "campaignName" TEXT NOT NULL,
    "campaignId" TEXT NOT NULL,
    "date" TIMESTAMP(3) NOT NULL,
    "tonicAccountId" INTEGER NOT NULL,
    "offerName" TEXT NOT NULL,
    "offerId" TEXT NOT NULL,
    "country" TEXT NOT NULL,
    "vertical" TEXT NOT NULL,
    "isSentToBinom" BOOLEAN NOT NULL DEFAULT false,
    "isFinal" BOOLEAN NOT NULL,
    "isNeedUpdate" BOOLEAN NOT NULL,
    "userId" INTEGER,

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

-- CreateTable
CREATE TABLE "DomainActiveLead" (
    "id" SERIAL NOT NULL,
    "clicks" INTEGER NOT NULL,
    "revenue" DOUBLE PRECISION NOT NULL,
    "trackerClickId" TEXT,
    "keyword" TEXT NOT NULL,
    "domainActiveAccountId" INTEGER NOT NULL,
    "userId" INTEGER,

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

-- CreateTable
CREATE TABLE "DomainActiveAccount" (
    "id" SERIAL NOT NULL,
    "name" TEXT NOT NULL,
    "token" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "binom_aff_network" (
    "id" SERIAL NOT NULL,
    "networkId" TEXT NOT NULL,
    "name" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "tonic_account" (
    "id" SERIAL NOT NULL,
    "createAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateAt" TIMESTAMP(3) NOT NULL,
    "consumerKey" TEXT NOT NULL,
    "consumerSecret" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "type" "TonicAccountType" NOT NULL,
    "startWorkingDate" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "telegram_account" (
    "id" SERIAL NOT NULL,
    "createDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateAt" TIMESTAMP(3) NOT NULL,
    "telegramId" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "worker_entity" (
    "id" SERIAL NOT NULL,
    "name" TEXT NOT NULL,
    "type" "WorkerType" NOT NULL,
    "minutes" INTEGER NOT NULL,
    "isRunning" BOOLEAN NOT NULL,
    "isActive" BOOLEAN NOT NULL,
    "fileName" TEXT NOT NULL,
    "nextRunTime" TIMESTAMP(3),
    "lastDuration" DOUBLE PRECISION,
    "error" BOOLEAN,
    "errorMessage" TEXT,

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

-- CreateTable
CREATE TABLE "log" (
    "id" SERIAL NOT NULL,
    "createDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "message" TEXT NOT NULL,
    "isError" BOOLEAN NOT NULL,
    "workerId" INTEGER,

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

-- CreateTable
CREATE TABLE "pixel" (
    "id" SERIAL NOT NULL,
    "pixelId" TEXT NOT NULL,
    "pixelToken" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "accountId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "_ownershipBeforeChange" (
    "A" INTEGER NOT NULL,
    "B" INTEGER NOT NULL
);

-- CreateTable
CREATE TABLE "_ownershipAfterChange" (
    "A" INTEGER NOT NULL,
    "B" INTEGER NOT NULL
);

-- CreateTable
CREATE TABLE "_BinomAffNetworkToBinomUser" (
    "A" INTEGER NOT NULL,
    "B" INTEGER NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "user_email_key" ON "user"("email");

-- CreateIndex
CREATE UNIQUE INDEX "user_binomUserId_key" ON "user"("binomUserId");

-- CreateIndex
CREATE UNIQUE INDEX "user_telegramUserId_key" ON "user"("telegramUserId");

-- CreateIndex
CREATE UNIQUE INDEX "user_billingAccountId_key" ON "user"("billingAccountId");

-- CreateIndex
CREATE UNIQUE INDEX "tiktok_campaign_tikTokCampaignId_accountId_key" ON "tiktok_campaign"("tikTokCampaignId", "accountId");

-- CreateIndex
CREATE UNIQUE INDEX "tiktok_adgroup_tikTokAdgroupId_tikTokCampaignId_key" ON "tiktok_adgroup"("tikTokAdgroupId", "tikTokCampaignId");

-- CreateIndex
CREATE UNIQUE INDEX "tiktok_ad_tikTokAdId_tikTokAdgroupId_key" ON "tiktok_ad"("tikTokAdId", "tikTokAdgroupId");

-- CreateIndex
CREATE UNIQUE INDEX "tiktok_ad_stat_date_adId_key" ON "tiktok_ad_stat"("date", "adId");

-- CreateIndex
CREATE UNIQUE INDEX "facebook_campaign_facebookAccountId_facebookCampaignId_key" ON "facebook_campaign"("facebookAccountId", "facebookCampaignId");

-- CreateIndex
CREATE UNIQUE INDEX "facebook_adgroup_facebookAdgroupId_facebookCampaignId_key" ON "facebook_adgroup"("facebookAdgroupId", "facebookCampaignId");

-- CreateIndex
CREATE UNIQUE INDEX "facebook_ad_facebookAdId_facebookAdgroupId_key" ON "facebook_ad"("facebookAdId", "facebookAdgroupId");

-- CreateIndex
CREATE UNIQUE INDEX "tonic_campaign_pixelId_key" ON "tonic_campaign"("pixelId");

-- CreateIndex
CREATE UNIQUE INDEX "account_accountId_type_key" ON "account"("accountId", "type");

-- CreateIndex
CREATE UNIQUE INDEX "agency_name_key" ON "agency"("name");

-- CreateIndex
CREATE UNIQUE INDEX "tonic_lead_campaignId_trackerClickId_date_key" ON "tonic_lead"("campaignId", "trackerClickId", "date");

-- CreateIndex
CREATE UNIQUE INDEX "tonic_account_type_key" ON "tonic_account"("type");

-- CreateIndex
CREATE UNIQUE INDEX "pixel_pixelId_accountId_key" ON "pixel"("pixelId", "accountId");

-- CreateIndex
CREATE UNIQUE INDEX "_ownershipBeforeChange_AB_unique" ON "_ownershipBeforeChange"("A", "B");

-- CreateIndex
CREATE INDEX "_ownershipBeforeChange_B_index" ON "_ownershipBeforeChange"("B");

-- CreateIndex
CREATE UNIQUE INDEX "_ownershipAfterChange_AB_unique" ON "_ownershipAfterChange"("A", "B");

-- CreateIndex
CREATE INDEX "_ownershipAfterChange_B_index" ON "_ownershipAfterChange"("B");

-- CreateIndex
CREATE UNIQUE INDEX "_BinomAffNetworkToBinomUser_AB_unique" ON "_BinomAffNetworkToBinomUser"("A", "B");

-- CreateIndex
CREATE INDEX "_BinomAffNetworkToBinomUser_B_index" ON "_BinomAffNetworkToBinomUser"("B");

-- AddForeignKey
ALTER TABLE "user" ADD CONSTRAINT "user_teamId_fkey" FOREIGN KEY ("teamId") REFERENCES "team"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user" ADD CONSTRAINT "user_binomUserId_fkey" FOREIGN KEY ("binomUserId") REFERENCES "binom_user"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user" ADD CONSTRAINT "user_telegramUserId_fkey" FOREIGN KEY ("telegramUserId") REFERENCES "telegram_account"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user" ADD CONSTRAINT "user_referalOwnerId_fkey" FOREIGN KEY ("referalOwnerId") REFERENCES "user"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user" ADD CONSTRAINT "user_billingAccountId_fkey" FOREIGN KEY ("billingAccountId") REFERENCES "billing_account"("id") ON DELETE SET NULL ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "tiktok_adgroup" ADD CONSTRAINT "tiktok_adgroup_campaignId_fkey" FOREIGN KEY ("campaignId") REFERENCES "tiktok_campaign"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tiktok_ad" ADD CONSTRAINT "tiktok_ad_adgroupId_fkey" FOREIGN KEY ("adgroupId") REFERENCES "tiktok_adgroup"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tiktok_ad_stat" ADD CONSTRAINT "tiktok_ad_stat_adId_fkey" FOREIGN KEY ("adId") REFERENCES "tiktok_ad"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "facebook_adgroup" ADD CONSTRAINT "facebook_adgroup_campaignId_fkey" FOREIGN KEY ("campaignId") REFERENCES "facebook_campaign"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "facebook_ad" ADD CONSTRAINT "facebook_ad_adgroupId_fkey" FOREIGN KEY ("adgroupId") REFERENCES "facebook_adgroup"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tonic_campaign" ADD CONSTRAINT "tonic_campaign_pixelId_fkey" FOREIGN KEY ("pixelId") REFERENCES "pixel"("id") ON DELETE SET NULL ON UPDATE CASCADE;

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

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

-- AddForeignKey
ALTER TABLE "account" ADD CONSTRAINT "account_agencyId_fkey" FOREIGN KEY ("agencyId") REFERENCES "agency"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "account" ADD CONSTRAINT "account_businessManagerId_fkey" FOREIGN KEY ("businessManagerId") REFERENCES "business_manager"("id") ON DELETE SET NULL ON UPDATE CASCADE;

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

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

-- AddForeignKey
ALTER TABLE "tonic_lead" ADD CONSTRAINT "tonic_lead_tonicAccountId_fkey" FOREIGN KEY ("tonicAccountId") REFERENCES "tonic_account"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tonic_lead" ADD CONSTRAINT "tonic_lead_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "DomainActiveLead" ADD CONSTRAINT "DomainActiveLead_domainActiveAccountId_fkey" FOREIGN KEY ("domainActiveAccountId") REFERENCES "DomainActiveAccount"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "DomainActiveLead" ADD CONSTRAINT "DomainActiveLead_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "log" ADD CONSTRAINT "log_workerId_fkey" FOREIGN KEY ("workerId") REFERENCES "worker_entity"("id") ON DELETE SET NULL ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "_ownershipBeforeChange" ADD CONSTRAINT "_ownershipBeforeChange_A_fkey" FOREIGN KEY ("A") REFERENCES "account_ownership_change"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_ownershipBeforeChange" ADD CONSTRAINT "_ownershipBeforeChange_B_fkey" FOREIGN KEY ("B") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_ownershipAfterChange" ADD CONSTRAINT "_ownershipAfterChange_A_fkey" FOREIGN KEY ("A") REFERENCES "account_ownership_change"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_ownershipAfterChange" ADD CONSTRAINT "_ownershipAfterChange_B_fkey" FOREIGN KEY ("B") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_BinomAffNetworkToBinomUser" ADD CONSTRAINT "_BinomAffNetworkToBinomUser_A_fkey" FOREIGN KEY ("A") REFERENCES "binom_aff_network"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_BinomAffNetworkToBinomUser" ADD CONSTRAINT "_BinomAffNetworkToBinomUser_B_fkey" FOREIGN KEY ("B") REFERENCES "binom_user"("id") ON DELETE CASCADE ON UPDATE CASCADE;
