-- CreateEnum
CREATE TYPE "StreamIssueModelName" AS ENUM ('StreamIssue');

-- CreateTable
CREATE TABLE "StreamIssue" (
    "modelName" "StreamIssueModelName" NOT NULL DEFAULT 'StreamIssue',
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "amount" INTEGER NOT NULL,
    "destinationFinancialAccountId" UUID NOT NULL,
    "withdrawnAt" DATE NOT NULL,
    "loanId" UUID NOT NULL,
    "userId" UUID NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "perkInstanceId" UUID NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "StreamIssue_loanId_key" ON "StreamIssue"("loanId");

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

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

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

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