/*
  Warnings:

  - You are about to drop the column `primary_contact_type` on the `patient_profiles` table. All the data in the column will be lost.

*/
-- AlterTable
ALTER TABLE "patient_profiles" DROP COLUMN "primary_contact_type";

-- CreateTable
CREATE TABLE "patient_relations" (
    "id" UUID NOT NULL,
    "host_id" UUID NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "patient_id" UUID NOT NULL,
    "related_external_id" TEXT NOT NULL,
    "related_patient_id" UUID,
    "relationship" TEXT NOT NULL,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMPTZ(3) NOT NULL,

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

-- CreateIndex
CREATE INDEX "patient_relations_patient_id_idx" ON "patient_relations"("patient_id");

-- CreateIndex
CREATE INDEX "patient_relations_related_patient_id_idx" ON "patient_relations"("related_patient_id");

-- CreateIndex
CREATE UNIQUE INDEX "patient_relations_patient_id_related_external_id_relationsh_key" ON "patient_relations"("patient_id", "related_external_id", "relationship");

-- AddForeignKey
ALTER TABLE "patient_relations" ADD CONSTRAINT "patient_relations_patient_id_fkey" FOREIGN KEY ("patient_id") REFERENCES "patients"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "patient_relations" ADD CONSTRAINT "patient_relations_related_patient_id_fkey" FOREIGN KEY ("related_patient_id") REFERENCES "patients"("id") ON DELETE SET NULL ON UPDATE CASCADE;
