/*
  Warnings:

  - You are about to drop the column `directDepositConnected` on the `Member` table. All the data in the column will be lost.

*/
-- CreateEnum
CREATE TYPE "DirectDepositAllocationType" AS ENUM ('Amount', 'None', 'Percent');

-- CreateEnum
CREATE TYPE "RoutedTransactionStatus" AS ENUM ('Created', 'Routing', 'Failed', 'Routed');

-- CreateEnum
CREATE TYPE "RoutedTransactionModelName" AS ENUM ('RoutedTransaction');

-- CreateEnum
CREATE TYPE "RoutedTransferModelName" AS ENUM ('RoutedTransfer');

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

-- AlterTable
ALTER TABLE "Member" DROP COLUMN "directDepositConnected",
ADD COLUMN     "directDepositAllocationAmount" INTEGER,
ADD COLUMN     "directDepositAllocationPercent" DOUBLE PRECISION,
ADD COLUMN     "directDepositAllocationType" "DirectDepositAllocationType" NOT NULL DEFAULT 'None';

-- AlterTable
ALTER TABLE "Organization" ADD COLUMN     "directDepositSubstring" TEXT NOT NULL DEFAULT '';

-- AlterTable
ALTER TABLE "Transfer" ADD COLUMN     "transmitOptions" JSONB NOT NULL DEFAULT '{}';

-- CreateTable
CREATE TABLE "RoutedTransaction" (
    "modelName" "RoutedTransactionModelName" NOT NULL DEFAULT 'RoutedTransaction',
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "status" "RoutedTransactionStatus" NOT NULL DEFAULT 'Created',
    "amount" INTEGER NOT NULL,
    "description" TEXT NOT NULL,
    "externalServiceId" TEXT NOT NULL,
    "externalServiceType" "ExternalServiceType" NOT NULL,
    "financialAccountId" UUID NOT NULL,
    "memberPaycheckId" UUID,
    "userId" UUID NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "RoutedTransfer" (
    "modelName" "RoutedTransferModelName" NOT NULL DEFAULT 'RoutedTransfer',
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "routedTransactionId" UUID NOT NULL,
    "perkInstanceId" UUID,
    "transferId" UUID NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "organizationId" UUID,
    "externalServiceReference" TEXT NOT NULL DEFAULT '',
    "userId" UUID NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "RoutedTransaction_externalServiceId_key" ON "RoutedTransaction"("externalServiceId");

-- CreateIndex
CREATE UNIQUE INDEX "RoutedTransaction_memberPaycheckId_key" ON "RoutedTransaction"("memberPaycheckId");

-- AddForeignKey
ALTER TABLE "RoutedTransaction" ADD CONSTRAINT "RoutedTransaction_financialAccountId_fkey" FOREIGN KEY ("financialAccountId") REFERENCES "FinancialAccount"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RoutedTransaction" ADD CONSTRAINT "RoutedTransaction_memberPaycheckId_fkey" FOREIGN KEY ("memberPaycheckId") REFERENCES "MemberPaycheck"("id") ON DELETE SET NULL ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "RoutedTransfer" ADD CONSTRAINT "RoutedTransfer_routedTransactionId_fkey" FOREIGN KEY ("routedTransactionId") REFERENCES "RoutedTransaction"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "RoutedTransfer" ADD CONSTRAINT "RoutedTransfer_perkInstanceId_fkey" FOREIGN KEY ("perkInstanceId") REFERENCES "PerkInstance"("id") ON DELETE SET NULL ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "RoutedTransfer" ADD CONSTRAINT "RoutedTransfer_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "Organization"("id") ON DELETE SET NULL ON UPDATE CASCADE;

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