-- AlterTable
ALTER TABLE "patients" ADD COLUMN "isWalkIn" BOOLEAN NOT NULL DEFAULT false;

CREATE INDEX "patients_createdByOrganizationId_isWalkIn_idx" ON "patients"("createdByOrganizationId", "isWalkIn");

CREATE UNIQUE INDEX "patients_one_walkin_per_org" ON "patients"("createdByOrganizationId") WHERE "isWalkIn" = true;

-- CreateEnum
CREATE TYPE "LabCaseOrigin" AS ENUM ('CLINIC_DISPATCH', 'LAB_INTERNAL');

-- AlterTable
ALTER TABLE "lab_cases" ALTER COLUMN "treatmentId" DROP NOT NULL;
ALTER TABLE "lab_cases" ALTER COLUMN "sortOrder" SET DEFAULT 0;
ALTER TABLE "lab_cases" ADD COLUMN "startedAt" TIMESTAMP(3);
ALTER TABLE "lab_cases" ADD COLUMN "origin" "LabCaseOrigin" NOT NULL DEFAULT 'CLINIC_DISPATCH';
ALTER TABLE "lab_cases" ADD COLUMN "referringClinicName" TEXT;
ALTER TABLE "lab_cases" ADD COLUMN "referringDentistName" TEXT;
ALTER TABLE "lab_cases" ADD COLUMN "patientDisplayName" TEXT;
ALTER TABLE "lab_cases" ADD COLUMN "patientDisplayMobile" TEXT;
ALTER TABLE "lab_cases" ADD COLUMN "partnerClinicOrganizationId" TEXT;

CREATE INDEX "lab_cases_destinationOrganizationId_origin_idx" ON "lab_cases"("destinationOrganizationId", "origin");
CREATE INDEX "lab_cases_partnerClinicOrganizationId_idx" ON "lab_cases"("partnerClinicOrganizationId");

ALTER TABLE "lab_cases" ADD CONSTRAINT "lab_cases_partnerClinicOrganizationId_fkey" FOREIGN KEY ("partnerClinicOrganizationId") REFERENCES "organizations"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- CreateTable
CREATE TABLE "lab_case_lines" (
    "id" TEXT NOT NULL,
    "labCaseId" TEXT NOT NULL,
    "clientKey" TEXT,
    "sortOrder" INTEGER NOT NULL,
    "treatmentType" TEXT NOT NULL DEFAULT 'prosthesis',
    "teeth" JSONB NOT NULL,
    "toothSelectionGroups" JSONB,
    "comment" TEXT,

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

CREATE INDEX "lab_case_lines_labCaseId_sortOrder_idx" ON "lab_case_lines"("labCaseId", "sortOrder");

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

-- Tooth prosthesis: nullable detail, lineId, sourceKey, treatmentType
DROP INDEX IF EXISTS "lab_case_tooth_prosthesis_labCaseId_treatmentDetailId_tooth_key";
ALTER TABLE "lab_case_tooth_prosthesis" ALTER COLUMN "treatmentDetailId" DROP NOT NULL;
ALTER TABLE "lab_case_tooth_prosthesis" ADD COLUMN "lineId" TEXT;
ALTER TABLE "lab_case_tooth_prosthesis" ADD COLUMN "sourceKey" TEXT;
ALTER TABLE "lab_case_tooth_prosthesis" ADD COLUMN "treatmentType" TEXT NOT NULL DEFAULT 'prosthesis';

UPDATE "lab_case_tooth_prosthesis" SET "sourceKey" = "treatmentDetailId" WHERE "sourceKey" IS NULL AND "treatmentDetailId" IS NOT NULL;

ALTER TABLE "lab_case_tooth_prosthesis" ALTER COLUMN "sourceKey" SET NOT NULL;

CREATE UNIQUE INDEX "lab_case_tooth_prosthesis_labCaseId_sourceKey_tooth_key" ON "lab_case_tooth_prosthesis"("labCaseId", "sourceKey", "tooth");

ALTER TABLE "lab_case_tooth_prosthesis" ADD CONSTRAINT "lab_case_tooth_prosthesis_lineId_fkey" FOREIGN KEY ("lineId") REFERENCES "lab_case_lines"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- Tasks: nullable detail, lineId, sourceKey
ALTER TABLE "lab_case_tasks" DROP CONSTRAINT IF EXISTS "lab_case_tasks_case_detail_prosthesis_step_key";
DROP INDEX IF EXISTS "lab_case_tasks_case_detail_prosthesis_step_key";

ALTER TABLE "lab_case_tasks" ALTER COLUMN "treatmentDetailId" DROP NOT NULL;
ALTER TABLE "lab_case_tasks" ADD COLUMN "lineId" TEXT;
ALTER TABLE "lab_case_tasks" ADD COLUMN "sourceKey" TEXT;

UPDATE "lab_case_tasks" SET "sourceKey" = "treatmentDetailId" WHERE "sourceKey" IS NULL AND "treatmentDetailId" IS NOT NULL;

ALTER TABLE "lab_case_tasks" ALTER COLUMN "sourceKey" SET NOT NULL;

CREATE UNIQUE INDEX "lab_case_tasks_case_source_prosthesis_step_key" ON "lab_case_tasks"("labCaseId", "sourceKey", "prosthesisTypeCode", "stepOrder");

ALTER TABLE "lab_case_tasks" ADD CONSTRAINT "lab_case_tasks_lineId_fkey" FOREIGN KEY ("lineId") REFERENCES "lab_case_lines"("id") ON DELETE CASCADE ON UPDATE CASCADE;

CREATE INDEX "treatments_organizationId_providerUserId_treatmentAt_idx" ON "treatments"("organizationId", "providerUserId", "treatmentAt");

ALTER TABLE "treatment_detail_attachments" ADD COLUMN "treatmentId" TEXT;
CREATE INDEX "treatment_detail_attachments_treatmentId_detailClientKey_idx" ON "treatment_detail_attachments"("treatmentId", "detailClientKey");
