-- CreateEnum
CREATE TYPE "LoanDecisionVerdict" AS ENUM ('Approved', 'Rejected', 'InReview');

-- CreateEnum
CREATE TYPE "LoanApplicationModelName" AS ENUM ('LoanApplication');

-- CreateEnum
CREATE TYPE "LoanOfferModelName" AS ENUM ('LoanOffer');

-- CreateEnum
CREATE TYPE "LoanDecisionModelName" AS ENUM ('LoanDecision');

-- DropForeignKey
ALTER TABLE "LoanPromissoryNote" DROP CONSTRAINT "LoanPromissoryNote_loanId_fkey";

-- AlterTable
ALTER TABLE "LoanPromissoryNote" ADD COLUMN     "loanOfferId" UUID,
ALTER COLUMN "loanId" DROP NOT NULL;

-- CreateTable
CREATE TABLE "LoanApplication" (
    "modelName" "LoanApplicationModelName" NOT NULL DEFAULT 'LoanApplication',
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "data" JSONB NOT NULL,
    "reviewRequestedAt" TIMESTAMP(3),
    "reviewCompletedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "userId" UUID NOT NULL,
    "perkInstanceId" UUID NOT NULL,

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

-- CreateTable
CREATE TABLE "LoanOffer" (
    "modelName" "LoanOfferModelName" NOT NULL DEFAULT 'LoanOffer',
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "applicationId" UUID NOT NULL,
    "loanId" UUID,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "userId" UUID NOT NULL,
    "amount" INTEGER NOT NULL,
    "termInMonths" INTEGER NOT NULL,
    "originationFee" DECIMAL(9,8) NOT NULL,

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

-- CreateTable
CREATE TABLE "LoanDecision" (
    "modelName" "LoanDecisionModelName" NOT NULL DEFAULT 'LoanDecision',
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "offerId" UUID NOT NULL,
    "agentId" UUID,
    "verdict" "LoanDecisionVerdict" NOT NULL,
    "notes" TEXT NOT NULL DEFAULT '',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "LoanOffer_loanId_key" ON "LoanOffer"("loanId");

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

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

-- AddForeignKey
ALTER TABLE "LoanOffer" ADD CONSTRAINT "LoanOffer_applicationId_fkey" FOREIGN KEY ("applicationId") REFERENCES "LoanApplication"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "LoanOffer" ADD CONSTRAINT "LoanOffer_loanId_fkey" FOREIGN KEY ("loanId") REFERENCES "Loan"("id") ON DELETE SET NULL ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "LoanDecision" ADD CONSTRAINT "LoanDecision_offerId_fkey" FOREIGN KEY ("offerId") REFERENCES "LoanOffer"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "LoanDecision" ADD CONSTRAINT "LoanDecision_agentId_fkey" FOREIGN KEY ("agentId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "LoanPromissoryNote" ADD CONSTRAINT "LoanPromissoryNote_loanId_fkey" FOREIGN KEY ("loanId") REFERENCES "Loan"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "LoanPromissoryNote" ADD CONSTRAINT "LoanPromissoryNote_loanOfferId_fkey" FOREIGN KEY ("loanOfferId") REFERENCES "LoanOffer"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- Migrate existing loan perks to indicate that they're version 1.
UPDATE "PerkInstance"
SET "settings" = jsonb_set(
  "settings",
  '{loan,version}',
  to_jsonb(1),
  true
)
WHERE "settings" ->> 'type' = 'Loan';
