-- CreateEnum
CREATE TYPE "SaveMatchStatus" AS ENUM ('Creating', 'Valid', 'Invalid');

-- CreateEnum
CREATE TYPE "SaveVestStatus" AS ENUM ('Creating', 'Pending', 'Valid', 'Invalid');

-- CreateEnum
CREATE TYPE "SaveDepositModelName" AS ENUM ('SaveDeposit');

-- CreateEnum
CREATE TYPE "SaveWithdrawalModelName" AS ENUM ('SaveWithdrawal');

-- CreateEnum
CREATE TYPE "SaveMatchModelName" AS ENUM ('SaveMatch');

-- CreateEnum
CREATE TYPE "SaveVestModelName" AS ENUM ('SaveVest');

-- CreateTable
CREATE TABLE "SaveDeposit" (
    "modelName" "SaveDepositModelName" NOT NULL DEFAULT 'SaveDeposit',
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "amount" INTEGER NOT NULL,
    "routedTransferId" UUID NOT NULL,
    "saveMatchId" UUID NOT NULL,
    "userId" UUID NOT NULL,
    "perkInstanceId" UUID NOT NULL,
    "transferId" UUID NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "SaveWithdrawal" (
    "modelName" "SaveWithdrawalModelName" NOT NULL DEFAULT 'SaveWithdrawal',
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "amount" INTEGER NOT NULL,
    "saveVestId" UUID NOT NULL,
    "userId" UUID NOT NULL,
    "perkInstanceId" UUID NOT NULL,
    "transferId" UUID,
    "externalServiceType" "ExternalServiceType",
    "externalServiceId" TEXT NOT NULL DEFAULT '',
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "SaveMatch" (
    "modelName" "SaveMatchModelName" NOT NULL DEFAULT 'SaveMatch',
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "startAt" DATE NOT NULL,
    "endAt" DATE NOT NULL,
    "amount" INTEGER NOT NULL,
    "depositAmount" INTEGER NOT NULL,
    "status" "SaveMatchStatus" NOT NULL,
    "saveVestId" UUID NOT NULL,
    "userId" UUID NOT NULL,
    "perkInstanceId" UUID NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "SaveVest" (
    "modelName" "SaveVestModelName" NOT NULL DEFAULT 'SaveVest',
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "initialBalance" INTEGER NOT NULL,
    "startAt" DATE NOT NULL,
    "endAt" DATE NOT NULL,
    "status" "SaveVestStatus" NOT NULL,
    "amount" INTEGER NOT NULL,
    "userId" UUID NOT NULL,
    "perkInstanceId" UUID NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "transferId" UUID,

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

-- CreateIndex
CREATE INDEX "SaveWithdrawal_externalServiceId_idx" ON "SaveWithdrawal"("externalServiceId");

-- AddForeignKey
ALTER TABLE "SaveDeposit" ADD CONSTRAINT "SaveDeposit_routedTransferId_fkey" FOREIGN KEY ("routedTransferId") REFERENCES "RoutedTransfer"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SaveDeposit" ADD CONSTRAINT "SaveDeposit_saveMatchId_fkey" FOREIGN KEY ("saveMatchId") REFERENCES "SaveMatch"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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

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

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

-- AddForeignKey
ALTER TABLE "SaveWithdrawal" ADD CONSTRAINT "SaveWithdrawal_saveVestId_fkey" FOREIGN KEY ("saveVestId") REFERENCES "SaveVest"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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

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

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

-- AddForeignKey
ALTER TABLE "SaveMatch" ADD CONSTRAINT "SaveMatch_saveVestId_fkey" FOREIGN KEY ("saveVestId") REFERENCES "SaveVest"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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

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

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

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

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