-- CreateEnum
CREATE TYPE "AppReferralModelName" AS ENUM ('AppReferral');

-- CreateEnum
CREATE TYPE "AppReferralStatus" AS ENUM ('Pending', 'Accepted', 'Canceled');

-- CreateTable
CREATE TABLE "AppReferral" (
    "modelName" "AppReferralModelName" NOT NULL DEFAULT 'AppReferral',
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "perkInstanceId" UUID NOT NULL,
    "transferId" UUID,
    "referredMemberId" UUID,
    "status" "AppReferralStatus" NOT NULL DEFAULT 'Pending',
    "userId" UUID NOT NULL,
    "organizationId" UUID NOT NULL,

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

-- AddForeignKey
ALTER TABLE "AppReferral" ADD CONSTRAINT "AppReferral_perkInstanceId_fkey" FOREIGN KEY ("perkInstanceId") REFERENCES "PerkInstance"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AppReferral" ADD CONSTRAINT "AppReferral_transferId_fkey" FOREIGN KEY ("transferId") REFERENCES "Transfer"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AppReferral" ADD CONSTRAINT "AppReferral_referredMemberId_fkey" FOREIGN KEY ("referredMemberId") REFERENCES "Member"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AppReferral" ADD CONSTRAINT "AppReferral_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AppReferral" ADD CONSTRAINT "AppReferral_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
