-- CreateTable
CREATE TABLE "user_rule_profit_report" (
    "id" SERIAL NOT NULL,
    "userFinanceReportId" INTEGER NOT NULL,
    "ruleId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "user_rule_profit_report_entity" (
    "id" SERIAL NOT NULL,
    "targetProfit" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "bonusPercent" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "totalBonus" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "userRuleProfitReportId" INTEGER NOT NULL,
    "userId" INTEGER,
    "teamId" INTEGER,

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

-- AddForeignKey
ALTER TABLE "user_rule_profit_report" ADD CONSTRAINT "user_rule_profit_report_userFinanceReportId_fkey" FOREIGN KEY ("userFinanceReportId") REFERENCES "user_finance_report"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_rule_profit_report" ADD CONSTRAINT "user_rule_profit_report_ruleId_fkey" FOREIGN KEY ("ruleId") REFERENCES "tiered_payout_rule"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_rule_profit_report_entity" ADD CONSTRAINT "user_rule_profit_report_entity_userRuleProfitReportId_fkey" FOREIGN KEY ("userRuleProfitReportId") REFERENCES "user_rule_profit_report"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_rule_profit_report_entity" ADD CONSTRAINT "user_rule_profit_report_entity_userId_fkey" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_rule_profit_report_entity" ADD CONSTRAINT "user_rule_profit_report_entity_teamId_fkey" FOREIGN KEY ("teamId") REFERENCES "team"("id") ON DELETE SET NULL ON UPDATE CASCADE;
