/*
  Warnings:

  - You are about to drop the column `providerEntityId` on the `date_revenue` table. All the data in the column will be lost.
  - You are about to drop the column `providerEntityId` on the `tonic_campaign` table. All the data in the column will be lost.
  - You are about to drop the `provider_entity` table. If the table is not empty, all the data it contains will be lost.
  - A unique constraint covering the columns `[date,offerId]` on the table `date_revenue` will be added. If there are existing duplicate values, this will fail.
  - Added the required column `tonicHeadlineId` to the `tonic_campaign` table without a default value. This is not possible if the table is not empty.

*/
-- CreateEnum
CREATE TYPE "HeadlineStatus" AS ENUM ('ACTIVE', 'PENDING', 'ERROR', 'REJECTED');

-- DropForeignKey
ALTER TABLE "date_revenue" DROP CONSTRAINT "date_revenue_providerEntityId_fkey";

-- DropForeignKey
ALTER TABLE "provider_entity" DROP CONSTRAINT "provider_entity_providerAccountId_fkey";

-- DropForeignKey
ALTER TABLE "provider_entity" DROP CONSTRAINT "provider_entity_userId_fkey";

-- DropForeignKey
ALTER TABLE "tonic_campaign" DROP CONSTRAINT "tonic_campaign_providerEntityId_fkey";

-- DropIndex
DROP INDEX "date_revenue_date_providerEntityId_key";

-- DropIndex
DROP INDEX "tonic_campaign_providerAccountId_key";

-- DropIndex
DROP INDEX "tonic_campaign_providerEntityId_key";

-- AlterTable
ALTER TABLE "date_revenue" DROP COLUMN "providerEntityId";

-- AlterTable
ALTER TABLE "tonic_campaign" DROP COLUMN "providerEntityId",
ADD COLUMN     "tonicHeadlineId" INTEGER;

-- DropTable
DROP TABLE "provider_entity";

-- CreateTable
CREATE TABLE "language" (
    "id" SERIAL NOT NULL,
    "createDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateDate" TIMESTAMP(3) NOT NULL,
    "countryCode" TEXT NOT NULL,
    "countryName" TEXT NOT NULL,
    "tonicCountryCode" TEXT,

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

-- CreateTable
CREATE TABLE "tonic_headline" (
    "id" SERIAL NOT NULL,
    "createDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateDate" TIMESTAMP(3) NOT NULL,
    "tonicOfferId" INTEGER NOT NULL,
    "headline" TEXT,
    "teaser" TEXT,
    "externalId" TEXT,
    "requestId" INTEGER,
    "status" "HeadlineStatus" NOT NULL DEFAULT 'PENDING',
    "statusDescription" TEXT,
    "providerAccountId" INTEGER,
    "languageId" INTEGER NOT NULL,
    "countryId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "tonic_content_generation_phrase" (
    "id" SERIAL NOT NULL,
    "createDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateDate" TIMESTAMP(3) NOT NULL,
    "phrase" TEXT NOT NULL,
    "tonicHeadlineId" INTEGER NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "tonic_content_generation_phrase_tonicHeadlineId_key" ON "tonic_content_generation_phrase"("tonicHeadlineId");

-- CreateIndex
CREATE UNIQUE INDEX "date_revenue_date_offerId_key" ON "date_revenue"("date", "offerId");

-- AddForeignKey
ALTER TABLE "tonic_campaign" ADD CONSTRAINT "tonic_campaign_tonicHeadlineId_fkey" FOREIGN KEY ("tonicHeadlineId") REFERENCES "tonic_headline"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tonic_headline" ADD CONSTRAINT "tonic_headline_tonicOfferId_fkey" FOREIGN KEY ("tonicOfferId") REFERENCES "tonic_offer"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tonic_headline" ADD CONSTRAINT "tonic_headline_providerAccountId_fkey" FOREIGN KEY ("providerAccountId") REFERENCES "provider_account"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tonic_headline" ADD CONSTRAINT "tonic_headline_languageId_fkey" FOREIGN KEY ("languageId") REFERENCES "language"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tonic_headline" ADD CONSTRAINT "tonic_headline_countryId_fkey" FOREIGN KEY ("countryId") REFERENCES "tonic_country"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tonic_content_generation_phrase" ADD CONSTRAINT "tonic_content_generation_phrase_tonicHeadlineId_fkey" FOREIGN KEY ("tonicHeadlineId") REFERENCES "tonic_headline"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
