/*
  Warnings:

  - You are about to drop the column `hourlyRate` on the `team_finance_profile` table. All the data in the column will be lost.
  - You are about to drop the column `monthlyBaseSalary` on the `team_finance_profile` table. All the data in the column will be lost.
  - You are about to drop the column `userFinanceProfileType` on the `team_finance_profile` table. All the data in the column will be lost.
  - You are about to drop the `team_balance_default_setting` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `team_payout_default_setting` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `user_finance_report` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `user_finance_report_additional_expense` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `user_provider_revenue` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `user_rule_profit_report` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `user_rule_profit_report_entity` table. If the table is not empty, all the data it contains will be lost.
  - Changed the type of `userFinanceProfileType` on the `user_finance_profile` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.

*/
-- CreateEnum
CREATE TYPE "FinanceProfileType" AS ENUM ('FRONT_OFFICE', 'BACK_OFFICE');

-- DropForeignKey
ALTER TABLE "account_spend_report" DROP CONSTRAINT "account_spend_report_userFinanceReportId_fkey";

-- DropForeignKey
ALTER TABLE "team_balance_default_setting" DROP CONSTRAINT "team_balance_default_setting_teamId_fkey";

-- DropForeignKey
ALTER TABLE "team_payout_default_setting" DROP CONSTRAINT "team_payout_default_setting_financeProfileId_fkey";

-- DropForeignKey
ALTER TABLE "user_finance_report" DROP CONSTRAINT "user_finance_report_financeProfileId_fkey";

-- DropForeignKey
ALTER TABLE "user_finance_report" DROP CONSTRAINT "user_finance_report_totalFinanceReportId_fkey";

-- DropForeignKey
ALTER TABLE "user_finance_report_additional_expense" DROP CONSTRAINT "user_finance_report_additional_expense_financeReportId_fkey";

-- DropForeignKey
ALTER TABLE "user_provider_revenue" DROP CONSTRAINT "user_provider_revenue_financeReportId_fkey";

-- DropForeignKey
ALTER TABLE "user_provider_revenue" DROP CONSTRAINT "user_provider_revenue_providerAccountId_fkey";

-- DropForeignKey
ALTER TABLE "user_rule_profit_report" DROP CONSTRAINT "user_rule_profit_report_financeReportId_fkey";

-- DropForeignKey
ALTER TABLE "user_rule_profit_report" DROP CONSTRAINT "user_rule_profit_report_ruleId_fkey";

-- DropForeignKey
ALTER TABLE "user_rule_profit_report_entity" DROP CONSTRAINT "user_rule_profit_report_entity_ruleProfitReportId_fkey";

-- DropForeignKey
ALTER TABLE "user_rule_profit_report_entity" DROP CONSTRAINT "user_rule_profit_report_entity_setId_fkey";

-- AlterTable
ALTER TABLE "team_finance_profile" DROP COLUMN "hourlyRate",
DROP COLUMN "monthlyBaseSalary",
DROP COLUMN "userFinanceProfileType";

-- AlterTable
ALTER TABLE "user_finance_profile" ALTER COLUMN "hourlyRate" SET DEFAULT 0,
DROP COLUMN "userFinanceProfileType",
ADD COLUMN     "userFinanceProfileType" "FinanceProfileType" NOT NULL;

-- DropTable
DROP TABLE "team_balance_default_setting";

-- DropTable
DROP TABLE "team_payout_default_setting";

-- DropTable
DROP TABLE "user_finance_report";

-- DropTable
DROP TABLE "user_finance_report_additional_expense";

-- DropTable
DROP TABLE "user_provider_revenue";

-- DropTable
DROP TABLE "user_rule_profit_report";

-- DropTable
DROP TABLE "user_rule_profit_report_entity";

-- DropEnum
DROP TYPE "UserFinanceProfileType";

-- CreateTable
CREATE TABLE "finance_report" (
    "id" SERIAL NOT NULL,
    "totalFinanceReportId" INTEGER NOT NULL,
    "financeProfileId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "finance_report_additional_expense" (
    "id" SERIAL NOT NULL,
    "amount" DECIMAL(65,30) NOT NULL,
    "description" TEXT NOT NULL,
    "financeReportId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "provider_revenue" (
    "id" SERIAL NOT NULL,
    "clawback" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "revenue" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "financeReportId" INTEGER NOT NULL,
    "providerAccountId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "rule_profit_report" (
    "id" SERIAL NOT NULL,
    "financeReportId" INTEGER NOT NULL,
    "ruleId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "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,
    "ruleProfitReportId" INTEGER NOT NULL,
    "setId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "balance_settings" (
    "id" SERIAL NOT NULL,
    "currentToOutstandingPayoutDay" INTEGER NOT NULL,
    "currentToOutstandingPayoutAmount" DECIMAL(65,30) NOT NULL,
    "currentToOutstandingPayoutPercent" DECIMAL(65,30) NOT NULL,
    "financeProfileId" INTEGER NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "balance_settings_financeProfileId_currentToOutstandingPayou_key" ON "balance_settings"("financeProfileId", "currentToOutstandingPayoutDay");

-- AddForeignKey
ALTER TABLE "account_spend_report" ADD CONSTRAINT "account_spend_report_userFinanceReportId_fkey" FOREIGN KEY ("userFinanceReportId") REFERENCES "finance_report"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "finance_report" ADD CONSTRAINT "finance_report_totalFinanceReportId_fkey" FOREIGN KEY ("totalFinanceReportId") REFERENCES "total_finance_report"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "finance_report" ADD CONSTRAINT "finance_report_financeProfileId_fkey" FOREIGN KEY ("financeProfileId") REFERENCES "finance_profile"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "finance_report_additional_expense" ADD CONSTRAINT "finance_report_additional_expense_financeReportId_fkey" FOREIGN KEY ("financeReportId") REFERENCES "finance_report"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "provider_revenue" ADD CONSTRAINT "provider_revenue_financeReportId_fkey" FOREIGN KEY ("financeReportId") REFERENCES "finance_report"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "provider_revenue" ADD CONSTRAINT "provider_revenue_providerAccountId_fkey" FOREIGN KEY ("providerAccountId") REFERENCES "provider_account"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "rule_profit_report" ADD CONSTRAINT "rule_profit_report_financeReportId_fkey" FOREIGN KEY ("financeReportId") REFERENCES "finance_report"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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

-- AddForeignKey
ALTER TABLE "rule_profit_report_entity" ADD CONSTRAINT "rule_profit_report_entity_ruleProfitReportId_fkey" FOREIGN KEY ("ruleProfitReportId") REFERENCES "rule_profit_report"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "rule_profit_report_entity" ADD CONSTRAINT "rule_profit_report_entity_setId_fkey" FOREIGN KEY ("setId") REFERENCES "tiered_payout_target_set"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "balance_settings" ADD CONSTRAINT "balance_settings_financeProfileId_fkey" FOREIGN KEY ("financeProfileId") REFERENCES "finance_profile"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
