-- CreateEnum
CREATE TYPE "UserNotificationType" AS ENUM (
  'CASE_SENT',
  'CLINIC_COMMENT',
  'LAB_COMMENT',
  'LAB_COMMENT_CLINIC',
  'CASE_IMPORTANT',
  'TASK_COMPLETED',
  'TASK_ASSIGNED',
  'CONNECTION_REQUEST',
  'STAFF_INVITE'
);

-- CreateTable
CREATE TABLE "user_notifications" (
  "id" TEXT NOT NULL,
  "userId" TEXT NOT NULL,
  "organizationId" TEXT NOT NULL,
  "type" "UserNotificationType" NOT NULL,
  "actorUserId" TEXT,
  "payload" JSONB,
  "href" TEXT NOT NULL,
  "readAt" TIMESTAMP(3),
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE INDEX "user_notifications_userId_organizationId_createdAt_idx"
  ON "user_notifications"("userId", "organizationId", "createdAt");

CREATE INDEX "user_notifications_userId_organizationId_readAt_idx"
  ON "user_notifications"("userId", "organizationId", "readAt");

ALTER TABLE "user_notifications"
  ADD CONSTRAINT "user_notifications_userId_fkey"
  FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "user_notifications"
  ADD CONSTRAINT "user_notifications_actorUserId_fkey"
  FOREIGN KEY ("actorUserId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
