-- AlterTable
ALTER TABLE "Device" ADD COLUMN     "distalShuntDirection" TEXT,
ADD COLUMN     "implantCatalogId" INTEGER,
ADD COLUMN     "implantManufacturer" TEXT,
ADD COLUMN     "implantModel" TEXT,
ADD COLUMN     "implantName" TEXT,
ADD COLUMN     "implantNotes" TEXT,
ADD COLUMN     "initialPressure" INTEGER,
ADD COLUMN     "isAbandoned" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN     "isPressureAdjustable" BOOLEAN NOT NULL DEFAULT true,
ADD COLUMN     "labelImageUrl" TEXT,
ADD COLUMN     "proximalPunctureAreas" TEXT[] DEFAULT ARRAY[]::TEXT[],
ADD COLUMN     "shuntMode" TEXT,
ADD COLUMN     "surgeryId" INTEGER,
ADD COLUMN     "valvePlacementSites" TEXT[] DEFAULT ARRAY[]::TEXT[];

-- AlterTable
ALTER TABLE "Patient" ADD COLUMN     "inpatientNo" TEXT,
ADD COLUMN     "projectName" TEXT;

-- CreateTable
CREATE TABLE "PatientSurgery" (
    "id" SERIAL NOT NULL,
    "patientId" INTEGER NOT NULL,
    "surgeryDate" TIMESTAMP(3) NOT NULL,
    "surgeryName" TEXT NOT NULL,
    "surgeonName" TEXT NOT NULL,
    "preOpPressure" INTEGER,
    "primaryDisease" TEXT NOT NULL,
    "hydrocephalusTypes" TEXT[] DEFAULT ARRAY[]::TEXT[],
    "previousShuntSurgeryDate" TIMESTAMP(3),
    "preOpMaterials" JSONB,
    "notes" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "ImplantCatalog" (
    "id" SERIAL NOT NULL,
    "modelCode" TEXT NOT NULL,
    "manufacturer" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "hospitalId" INTEGER,
    "isPressureAdjustable" BOOLEAN NOT NULL DEFAULT true,

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

-- CreateIndex
CREATE INDEX "PatientSurgery_patientId_surgeryDate_idx" ON "PatientSurgery"("patientId", "surgeryDate");

-- CreateIndex
CREATE UNIQUE INDEX "ImplantCatalog_modelCode_key" ON "ImplantCatalog"("modelCode");

-- CreateIndex
CREATE INDEX "ImplantCatalog_hospitalId_idx" ON "ImplantCatalog"("hospitalId");

-- CreateIndex
CREATE INDEX "Device_surgeryId_idx" ON "Device"("surgeryId");

-- CreateIndex
CREATE INDEX "Device_implantCatalogId_idx" ON "Device"("implantCatalogId");

-- CreateIndex
CREATE INDEX "Device_patientId_isAbandoned_idx" ON "Device"("patientId", "isAbandoned");

-- CreateIndex
CREATE INDEX "Patient_inpatientNo_idx" ON "Patient"("inpatientNo");

-- AddForeignKey
ALTER TABLE "PatientSurgery" ADD CONSTRAINT "PatientSurgery_patientId_fkey" FOREIGN KEY ("patientId") REFERENCES "Patient"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ImplantCatalog" ADD CONSTRAINT "ImplantCatalog_hospitalId_fkey" FOREIGN KEY ("hospitalId") REFERENCES "Hospital"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Device" ADD CONSTRAINT "Device_surgeryId_fkey" FOREIGN KEY ("surgeryId") REFERENCES "PatientSurgery"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Device" ADD CONSTRAINT "Device_implantCatalogId_fkey" FOREIGN KEY ("implantCatalogId") REFERENCES "ImplantCatalog"("id") ON DELETE SET NULL ON UPDATE CASCADE;
