-- CreateTable
CREATE TABLE "plan_event_logs" (
    "id" UUID NOT NULL,
    "host_id" UUID NOT NULL,
    "tenant_id" TEXT NOT NULL,
    "plan_id" UUID NOT NULL,
    "patient_id" UUID NOT NULL,
    "event" TEXT NOT NULL,
    "assignee_user_id" TEXT,
    "actor_user_id" TEXT,
    "held_seconds" INTEGER,
    "reason" TEXT,
    "details" JSONB,
    "created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "plan_event_logs_plan_id_created_at_idx" ON "plan_event_logs"("plan_id", "created_at");

-- CreateIndex
CREATE INDEX "plan_event_logs_assignee_user_id_created_at_idx" ON "plan_event_logs"("assignee_user_id", "created_at");

-- CreateIndex
CREATE INDEX "plan_event_logs_patient_id_created_at_idx" ON "plan_event_logs"("patient_id", "created_at");

-- CreateIndex
CREATE INDEX "plan_event_logs_host_id_tenant_id_event_created_at_idx" ON "plan_event_logs"("host_id", "tenant_id", "event", "created_at");

-- AddForeignKey
ALTER TABLE "plan_event_logs" ADD CONSTRAINT "plan_event_logs_host_id_fkey" FOREIGN KEY ("host_id") REFERENCES "hosts"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
