-- Split treatment_cases into treatment_details + lab_cases (test data cleared).

DELETE FROM "treatment_case_sends";
DELETE FROM "treatment_case_attachments";
DELETE FROM "treatment_cases";
DELETE FROM "treatments";

DROP TABLE IF EXISTS "treatment_case_sends";
DROP TABLE IF EXISTS "treatment_case_attachments";
DROP TABLE IF EXISTS "treatment_cases";

CREATE TABLE "treatment_details" (
  "id" TEXT NOT NULL,
  "treatmentId" TEXT NOT NULL,
  "clientKey" TEXT,
  "sortOrder" INTEGER NOT NULL,
  "treatmentType" TEXT NOT NULL,
  "teeth" JSONB NOT NULL,
  "comment" TEXT,

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

CREATE TABLE "treatment_detail_attachments" (
  "id" TEXT NOT NULL,
  "detailId" TEXT,
  "appointmentId" TEXT,
  "detailClientKey" TEXT,
  "fileName" TEXT NOT NULL,
  "mimeType" TEXT NOT NULL,
  "sizeBytes" INTEGER NOT NULL,
  "storagePath" TEXT NOT NULL,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE TABLE "lab_cases" (
  "id" TEXT NOT NULL,
  "treatmentId" TEXT NOT NULL,
  "clientKey" TEXT,
  "sortOrder" INTEGER NOT NULL,
  "destinationOrganizationId" TEXT,
  "labComment" TEXT,
  "sentAt" TIMESTAMP(3),

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

CREATE TABLE "lab_case_details" (
  "labCaseId" TEXT NOT NULL,
  "treatmentDetailId" TEXT NOT NULL,

  CONSTRAINT "lab_case_details_pkey" PRIMARY KEY ("labCaseId", "treatmentDetailId")
);

CREATE TABLE "lab_case_sends" (
  "id" TEXT NOT NULL,
  "labCaseId" TEXT NOT NULL,
  "organizationId" TEXT NOT NULL,
  "sentAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE INDEX "treatment_details_treatmentId_sortOrder_idx" ON "treatment_details"("treatmentId", "sortOrder");
CREATE INDEX "treatment_detail_attachments_appointmentId_detailClientKey_idx" ON "treatment_detail_attachments"("appointmentId", "detailClientKey");
CREATE INDEX "treatment_detail_attachments_detailId_idx" ON "treatment_detail_attachments"("detailId");
CREATE INDEX "lab_cases_treatmentId_sortOrder_idx" ON "lab_cases"("treatmentId", "sortOrder");
CREATE UNIQUE INDEX "lab_case_details_treatmentDetailId_key" ON "lab_case_details"("treatmentDetailId");
CREATE UNIQUE INDEX "lab_case_sends_labCaseId_organizationId_key" ON "lab_case_sends"("labCaseId", "organizationId");

ALTER TABLE "treatment_details"
  ADD CONSTRAINT "treatment_details_treatmentId_fkey"
  FOREIGN KEY ("treatmentId") REFERENCES "treatments"("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "treatment_detail_attachments"
  ADD CONSTRAINT "treatment_detail_attachments_detailId_fkey"
  FOREIGN KEY ("detailId") REFERENCES "treatment_details"("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "lab_cases"
  ADD CONSTRAINT "lab_cases_treatmentId_fkey"
  FOREIGN KEY ("treatmentId") REFERENCES "treatments"("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "lab_case_details"
  ADD CONSTRAINT "lab_case_details_labCaseId_fkey"
  FOREIGN KEY ("labCaseId") REFERENCES "lab_cases"("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "lab_case_details"
  ADD CONSTRAINT "lab_case_details_treatmentDetailId_fkey"
  FOREIGN KEY ("treatmentDetailId") REFERENCES "treatment_details"("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "lab_case_sends"
  ADD CONSTRAINT "lab_case_sends_labCaseId_fkey"
  FOREIGN KEY ("labCaseId") REFERENCES "lab_cases"("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "lab_case_sends"
  ADD CONSTRAINT "lab_case_sends_organizationId_fkey"
  FOREIGN KEY ("organizationId") REFERENCES "organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;
