CREATE TYPE "ReportTemplateEntity" AS ENUM ('RESOURCE', 'PROJECT', 'ASSIGNMENT', 'RESOURCE_MONTH');

CREATE TABLE "report_templates" (
  "id" TEXT NOT NULL,
  "ownerId" TEXT NOT NULL,
  "name" TEXT NOT NULL,
  "description" TEXT,
  "entity" "ReportTemplateEntity" NOT NULL,
  "config" JSONB NOT NULL,
  "isShared" BOOLEAN NOT NULL DEFAULT false,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE INDEX "report_templates_ownerId_updatedAt_idx"
  ON "report_templates"("ownerId", "updatedAt");

CREATE UNIQUE INDEX "report_templates_ownerId_name_key"
  ON "report_templates"("ownerId", "name");

ALTER TABLE "report_templates"
  ADD CONSTRAINT "report_templates_ownerId_fkey"
  FOREIGN KEY ("ownerId") REFERENCES "users"("id")
  ON DELETE CASCADE ON UPDATE CASCADE;
