/*
  Warnings:

  - The values [TOBOOLA] on the enum `TonicAccountType` will be removed. If these variants are still used in the database, this will fail.
  - You are about to drop the `tiktok_ad_stat` table. If the table is not empty, all the data it contains will be lost.

*/
-- AlterEnum
BEGIN;
CREATE TYPE "TonicAccountType_new" AS ENUM ('FACEBOOK', 'TIKTOK', 'TABOOLA');
ALTER TABLE "tonic_account" ALTER COLUMN "type" TYPE "TonicAccountType_new" USING ("type"::text::"TonicAccountType_new");
ALTER TYPE "TonicAccountType" RENAME TO "TonicAccountType_old";
ALTER TYPE "TonicAccountType_new" RENAME TO "TonicAccountType";
DROP TYPE "TonicAccountType_old";
COMMIT;

-- DropForeignKey
ALTER TABLE "tiktok_ad_stat" DROP CONSTRAINT "tiktok_ad_stat_adId_fkey";

-- DropTable
DROP TABLE "tiktok_ad_stat";

-- CreateTable
CREATE TABLE "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,
    "adId" TEXT NOT NULL,
    "adgroupId" TEXT NOT NULL,
    "campaignId" TEXT NOT NULL,
    "accountId" TEXT NOT NULL,
    "tikTokAdIdRef" INTEGER NOT NULL,
    "fbAdIdRef" INTEGER NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "ad_stat_date_adId_key" ON "ad_stat"("date", "adId");

-- AddForeignKey
ALTER TABLE "ad_stat" ADD CONSTRAINT "ad_stat_tikTokAdIdRef_fkey" FOREIGN KEY ("tikTokAdIdRef") REFERENCES "tiktok_ad"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ad_stat" ADD CONSTRAINT "ad_stat_fbAdIdRef_fkey" FOREIGN KEY ("fbAdIdRef") REFERENCES "facebook_ad"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
