-- CreateEnum
CREATE TYPE "SessionAuthStatus" AS ENUM ('Unauthenticated', 'AwaitingOtp', 'AwaitingPin', 'Authenticated');

-- CreateEnum
CREATE TYPE "AuthFactorModelName" AS ENUM ('AuthFactor');

-- CreateEnum
CREATE TYPE "AuthTokenModelName" AS ENUM ('AuthToken');

-- AlterTable
ALTER TABLE "Session" ADD COLUMN     "authFactorId" UUID,
ADD COLUMN     "authStatus" "SessionAuthStatus" NOT NULL DEFAULT 'Unauthenticated',
ADD COLUMN     "inviteId" UUID;

-- AlterTable
ALTER TABLE "User" ADD COLUMN     "hashedPin" TEXT NOT NULL DEFAULT '';

-- CreateTable
CREATE TABLE "AuthFactor" (
    "modelName" "AuthFactorModelName" NOT NULL DEFAULT 'AuthFactor',
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "phone" TEXT NOT NULL DEFAULT '',
    "email" TEXT NOT NULL DEFAULT '',
    "isVerified" BOOLEAN NOT NULL DEFAULT false,
    "isHidden" BOOLEAN NOT NULL DEFAULT false,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "userId" UUID,

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

-- CreateTable
CREATE TABLE "AuthToken" (
    "modelName" "AuthTokenModelName" NOT NULL DEFAULT 'AuthToken',
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "factorId" UUID NOT NULL,
    "hashedToken" TEXT NOT NULL,
    "expiresAt" TIMESTAMP(3) NOT NULL,

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

-- AddForeignKey
ALTER TABLE "AuthFactor" ADD CONSTRAINT "AuthFactor_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AuthToken" ADD CONSTRAINT "AuthToken_factorId_fkey" FOREIGN KEY ("factorId") REFERENCES "AuthFactor"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Session" ADD CONSTRAINT "Session_authFactorId_fkey" FOREIGN KEY ("authFactorId") REFERENCES "AuthFactor"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Session" ADD CONSTRAINT "Session_inviteId_fkey" FOREIGN KEY ("inviteId") REFERENCES "Invite"("id") ON DELETE SET NULL ON UPDATE CASCADE;
