-- CreateEnum
CREATE TYPE "TieredPayoutRuleType" AS ENUM ('TEAM', 'USER');

-- CreateTable
CREATE TABLE "tiered_payout_rule" (
    "id" SERIAL NOT NULL,
    "createDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateDate" TIMESTAMP(3) NOT NULL,
    "name" TEXT NOT NULL,
    "type" "TieredPayoutRuleType" NOT NULL,
    "userFinanceProfileId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "tiered_payout_rule_data" (
    "id" SERIAL NOT NULL,
    "createDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateDate" TIMESTAMP(3) NOT NULL,
    "percent" DOUBLE PRECISION,
    "thereshhold" DOUBLE PRECISION,
    "tieredPayoutRuleId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "tiered_payout_rule_to_user" (
    "id" SERIAL NOT NULL,
    "tieredPayoutRuleId" INTEGER NOT NULL,
    "userId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "tiered_payout_rule_to_team" (
    "id" SERIAL NOT NULL,
    "tieredPayoutRuleId" INTEGER NOT NULL,
    "teamId" INTEGER NOT NULL,

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

-- AddForeignKey
ALTER TABLE "tiered_payout_rule" ADD CONSTRAINT "tiered_payout_rule_userFinanceProfileId_fkey" FOREIGN KEY ("userFinanceProfileId") REFERENCES "user_finance_profile"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tiered_payout_rule_data" ADD CONSTRAINT "tiered_payout_rule_data_tieredPayoutRuleId_fkey" FOREIGN KEY ("tieredPayoutRuleId") REFERENCES "tiered_payout_rule"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tiered_payout_rule_to_user" ADD CONSTRAINT "tiered_payout_rule_to_user_tieredPayoutRuleId_fkey" FOREIGN KEY ("tieredPayoutRuleId") REFERENCES "tiered_payout_rule"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tiered_payout_rule_to_user" ADD CONSTRAINT "tiered_payout_rule_to_user_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tiered_payout_rule_to_team" ADD CONSTRAINT "tiered_payout_rule_to_team_tieredPayoutRuleId_fkey" FOREIGN KEY ("tieredPayoutRuleId") REFERENCES "tiered_payout_rule"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "tiered_payout_rule_to_team" ADD CONSTRAINT "tiered_payout_rule_to_team_teamId_fkey" FOREIGN KEY ("teamId") REFERENCES "team"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
