-- CreateEnum
CREATE TYPE "TransferStatus" AS ENUM ('Created', 'Pending', 'Completed', 'Failed');

-- CreateEnum
CREATE TYPE "TransferModelName" AS ENUM ('Transfer');

-- CreateEnum
CREATE TYPE "LoanModelName" AS ENUM ('Loan');

-- CreateEnum
CREATE TYPE "LoanPromissoryNoteModelName" AS ENUM ('LoanPromissoryNote');

-- CreateEnum
CREATE TYPE "LoanRepaymentModelName" AS ENUM ('LoanRepayment');

-- AlterEnum
ALTER TYPE "ExternalServiceType" ADD VALUE 'PaloloLoans';

-- CreateTable
CREATE TABLE "Transfer" (
    "modelName" "TransferModelName" NOT NULL DEFAULT 'Transfer',
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "effectiveUserId" UUID,
    "sourceFinancialAccountId" UUID NOT NULL,
    "destinationFinancialAccountId" UUID NOT NULL,
    "amount" INTEGER NOT NULL,
    "description" TEXT NOT NULL,
    "status" "TransferStatus" NOT NULL DEFAULT 'Created',
    "occurredAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "externalServiceType" "ExternalServiceType",
    "externalServiceId" TEXT NOT NULL DEFAULT '',
    "externalServiceReference" TEXT NOT NULL DEFAULT '',

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

-- CreateTable
CREATE TABLE "Loan" (
    "modelName" "LoanModelName" NOT NULL DEFAULT 'Loan',
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "perkInstanceId" UUID NOT NULL,
    "isApproved" BOOLEAN NOT NULL,
    "amount" INTEGER NOT NULL,
    "originationFee" DOUBLE PRECISION NOT NULL,
    "termInMonths" INTEGER NOT NULL,
    "transferId" UUID,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "userId" UUID NOT NULL,

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

-- CreateTable
CREATE TABLE "LoanPromissoryNote" (
    "modelName" "LoanPromissoryNoteModelName" NOT NULL DEFAULT 'LoanPromissoryNote',
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "html" TEXT NOT NULL,
    "acceptedAt" TIMESTAMP(3),
    "loanId" UUID NOT NULL,
    "userId" UUID NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "LoanRepayment" (
    "modelName" "LoanRepaymentModelName" NOT NULL DEFAULT 'LoanRepayment',
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "loanId" UUID NOT NULL,
    "transferId" UUID NOT NULL,
    "userId" UUID NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- AddForeignKey
ALTER TABLE "Transfer" ADD CONSTRAINT "Transfer_effectiveUserId_fkey" FOREIGN KEY ("effectiveUserId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Transfer" ADD CONSTRAINT "Transfer_sourceFinancialAccountId_fkey" FOREIGN KEY ("sourceFinancialAccountId") REFERENCES "FinancialAccount"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Transfer" ADD CONSTRAINT "Transfer_destinationFinancialAccountId_fkey" FOREIGN KEY ("destinationFinancialAccountId") REFERENCES "FinancialAccount"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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

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

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

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

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

-- AddForeignKey
ALTER TABLE "LoanRepayment" ADD CONSTRAINT "LoanRepayment_loanId_fkey" FOREIGN KEY ("loanId") REFERENCES "Loan"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "LoanRepayment" ADD CONSTRAINT "LoanRepayment_transferId_fkey" FOREIGN KEY ("transferId") REFERENCES "Transfer"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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