/*
  Warnings:

  - You are about to drop the column `organizationId` on the `TemplateInfo` table. All the data in the column will be lost.
  - You are about to drop the column `badgeId` on the `templateProduct` table. All the data in the column will be lost.
  - You are about to drop the column `organizationId` on the `templateProduct` table. All the data in the column will be lost.
  - You are about to drop the `Badge` table. If the table is not empty, all the data it contains will be lost.
  - A unique constraint covering the columns `[clonedTemplateId]` on the table `TemplateInfo` will be added. If there are existing duplicate values, this will fail.
  - Added the required column `clonedTemplateId` to the `TemplateInfo` table without a default value. This is not possible if the table is not empty.
  - Added the required column `clonedTemplateId` to the `templateProduct` table without a default value. This is not possible if the table is not empty.

*/
-- DropForeignKey
ALTER TABLE "TemplateInfo" DROP CONSTRAINT "TemplateInfo_organizationId_fkey";

-- DropForeignKey
ALTER TABLE "TemplateInfo" DROP CONSTRAINT "TemplateInfo_templateId_fkey";

-- DropForeignKey
ALTER TABLE "templateProduct" DROP CONSTRAINT "templateProduct_badgeId_fkey";

-- DropForeignKey
ALTER TABLE "templateProduct" DROP CONSTRAINT "templateProduct_organizationId_fkey";

-- DropForeignKey
ALTER TABLE "templateProduct" DROP CONSTRAINT "templateProduct_templateId_fkey";

-- DropForeignKey
ALTER TABLE "templates" DROP CONSTRAINT "templates_organizationTypeId_fkey";

-- DropIndex
DROP INDEX "templateProduct_organizationId_idx";

-- DropIndex
DROP INDEX "templateProduct_templateId_idx";

-- AlterTable
ALTER TABLE "TemplateInfo" DROP COLUMN "organizationId",
ADD COLUMN     "backgroundColor" TEXT,
ADD COLUMN     "backgroundImage" TEXT,
ADD COLUMN     "clonedTemplateId" INTEGER NOT NULL,
ADD COLUMN     "font" TEXT,
ADD COLUMN     "textColor" TEXT,
ALTER COLUMN "title" DROP NOT NULL;

-- AlterTable
ALTER TABLE "templateProduct" DROP COLUMN "badgeId",
DROP COLUMN "organizationId",
ADD COLUMN     "badge" TEXT,
ADD COLUMN     "clonedTemplateId" INTEGER NOT NULL,
ALTER COLUMN "templateId" DROP NOT NULL;

-- DropTable
DROP TABLE "Badge";

-- CreateTable
CREATE TABLE "ClonedTemplate" (
    "id" SERIAL NOT NULL,
    "templateId" INTEGER NOT NULL,
    "isPublished" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "organizationId" INTEGER NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "TemplateInfo_clonedTemplateId_key" ON "TemplateInfo"("clonedTemplateId");

-- CreateIndex
CREATE INDEX "templateProduct_productId_idx" ON "templateProduct"("productId");

-- CreateIndex
CREATE INDEX "templateProduct_clonedTemplateId_idx" ON "templateProduct"("clonedTemplateId");

-- AddForeignKey
ALTER TABLE "templates" ADD CONSTRAINT "templates_organizationTypeId_fkey" FOREIGN KEY ("organizationTypeId") REFERENCES "organization_types"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ClonedTemplate" ADD CONSTRAINT "ClonedTemplate_templateId_fkey" FOREIGN KEY ("templateId") REFERENCES "templates"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ClonedTemplate" ADD CONSTRAINT "ClonedTemplate_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TemplateInfo" ADD CONSTRAINT "TemplateInfo_clonedTemplateId_fkey" FOREIGN KEY ("clonedTemplateId") REFERENCES "ClonedTemplate"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TemplateInfo" ADD CONSTRAINT "TemplateInfo_templateId_fkey" FOREIGN KEY ("templateId") REFERENCES "templates"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "templateProduct" ADD CONSTRAINT "templateProduct_clonedTemplateId_fkey" FOREIGN KEY ("clonedTemplateId") REFERENCES "ClonedTemplate"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "templateProduct" ADD CONSTRAINT "templateProduct_productId_fkey" FOREIGN KEY ("productId") REFERENCES "products"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "templateProduct" ADD CONSTRAINT "templateProduct_templateId_fkey" FOREIGN KEY ("templateId") REFERENCES "templates"("id") ON DELETE SET NULL ON UPDATE CASCADE;
