/*
  Warnings:

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

*/
-- DropForeignKey
ALTER TABLE "ClonedTemplate" DROP CONSTRAINT "ClonedTemplate_organizationId_fkey";

-- DropForeignKey
ALTER TABLE "ClonedTemplate" DROP CONSTRAINT "ClonedTemplate_templateId_fkey";

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

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

-- DropIndex
DROP INDEX "TemplateInfo_clonedTemplateId_key";

-- DropIndex
DROP INDEX "templateProduct_clonedTemplateId_idx";

-- AlterTable
ALTER TABLE "TemplateInfo" DROP COLUMN "clonedTemplateId",
ADD COLUMN     "organizationTemplateId" INTEGER NOT NULL;

-- AlterTable
ALTER TABLE "templateProduct" DROP COLUMN "clonedTemplateId",
ADD COLUMN     "organizationTemplateId" INTEGER NOT NULL;

-- DropTable
DROP TABLE "ClonedTemplate";

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

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

-- CreateIndex
CREATE UNIQUE INDEX "TemplateInfo_organizationTemplateId_key" ON "TemplateInfo"("organizationTemplateId");

-- CreateIndex
CREATE INDEX "templateProduct_organizationTemplateId_idx" ON "templateProduct"("organizationTemplateId");

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

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

-- AddForeignKey
ALTER TABLE "TemplateInfo" ADD CONSTRAINT "TemplateInfo_organizationTemplateId_fkey" FOREIGN KEY ("organizationTemplateId") REFERENCES "OrganizationTemplate"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "templateProduct" ADD CONSTRAINT "templateProduct_organizationTemplateId_fkey" FOREIGN KEY ("organizationTemplateId") REFERENCES "OrganizationTemplate"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
