-- Prosthesis catalog refactor: drop treatment workflow steps, add prosthesis catalog tables

CREATE TYPE "CatalogEntityKind" AS ENUM ('TREATMENT_TYPE', 'PROSTHESIS_TYPE', 'LAB_WORKFLOW_STEP');

ALTER TABLE "treatment_types" ADD COLUMN "isActive" BOOLEAN NOT NULL DEFAULT true;

DROP TABLE IF EXISTS "treatment_workflow_steps";

CREATE TABLE "catalog_translations" (
  "id" TEXT NOT NULL,
  "entityKind" "CatalogEntityKind" NOT NULL,
  "entityCode" TEXT NOT NULL,
  "locale" TEXT NOT NULL,
  "label" TEXT NOT NULL,

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

CREATE UNIQUE INDEX "catalog_translations_entityKind_entityCode_locale_key"
  ON "catalog_translations"("entityKind", "entityCode", "locale");

CREATE TABLE "prosthesis_types" (
  "id" TEXT NOT NULL,
  "code" TEXT NOT NULL,
  "sortOrder" INTEGER NOT NULL DEFAULT 0,
  "isActive" BOOLEAN NOT NULL DEFAULT true,
  "skipPackingShipping" BOOLEAN NOT NULL DEFAULT false,

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

CREATE UNIQUE INDEX "prosthesis_types_code_key" ON "prosthesis_types"("code");

CREATE TABLE "lab_workflow_steps" (
  "id" TEXT NOT NULL,
  "code" TEXT NOT NULL,
  "sortOrder" INTEGER NOT NULL DEFAULT 0,

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

CREATE UNIQUE INDEX "lab_workflow_steps_code_key" ON "lab_workflow_steps"("code");

CREATE TABLE "prosthesis_type_steps" (
  "id" TEXT NOT NULL,
  "prosthesisTypeId" TEXT NOT NULL,
  "labWorkflowStepId" TEXT NOT NULL,
  "stepOrder" INTEGER NOT NULL,

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

CREATE UNIQUE INDEX "prosthesis_type_steps_prosthesisTypeId_stepOrder_key"
  ON "prosthesis_type_steps"("prosthesisTypeId", "stepOrder");

CREATE UNIQUE INDEX "prosthesis_type_steps_prosthesisTypeId_labWorkflowStepId_key"
  ON "prosthesis_type_steps"("prosthesisTypeId", "labWorkflowStepId");

ALTER TABLE "prosthesis_type_steps"
  ADD CONSTRAINT "prosthesis_type_steps_prosthesisTypeId_fkey"
  FOREIGN KEY ("prosthesisTypeId") REFERENCES "prosthesis_types"("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "prosthesis_type_steps"
  ADD CONSTRAINT "prosthesis_type_steps_labWorkflowStepId_fkey"
  FOREIGN KEY ("labWorkflowStepId") REFERENCES "lab_workflow_steps"("id") ON DELETE CASCADE ON UPDATE CASCADE;

CREATE TABLE "lab_case_tooth_prosthesis" (
  "id" TEXT NOT NULL,
  "labCaseId" TEXT NOT NULL,
  "treatmentDetailId" TEXT NOT NULL,
  "tooth" TEXT NOT NULL,
  "prosthesisTypeCode" TEXT NOT NULL,

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

CREATE UNIQUE INDEX "lab_case_tooth_prosthesis_labCaseId_treatmentDetailId_tooth_key"
  ON "lab_case_tooth_prosthesis"("labCaseId", "treatmentDetailId", "tooth");

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

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

-- Lab case tasks: add prosthesis fields and update unique constraint
ALTER TABLE "lab_case_tasks" ADD COLUMN "prosthesisTypeCode" TEXT NOT NULL DEFAULT '';
ALTER TABLE "lab_case_tasks" ADD COLUMN "workflowStepCode" TEXT NOT NULL DEFAULT '';

DROP INDEX IF EXISTS "lab_case_tasks_labCaseId_tooth_treatmentType_stepOrder_key";

CREATE UNIQUE INDEX "lab_case_tasks_labCaseId_treatmentDetailId_tooth_stepOrder_key"
  ON "lab_case_tasks"("labCaseId", "treatmentDetailId", "tooth", "stepOrder");
