/*
  Warnings:

  - You are about to drop the column `userFinanceProfileId` on the `tiered_payout_rule` table. All the data in the column will be lost.
  - You are about to drop the column `thereshhold` on the `tiered_payout_rule_data` table. All the data in the column will be lost.
  - You are about to drop the column `teamId` on the `tiered_payout_rule_target` table. All the data in the column will be lost.
  - You are about to drop the column `userId` on the `tiered_payout_rule_target` table. All the data in the column will be lost.
  - You are about to drop the column `active` on the `user_finance_profile` table. All the data in the column will be lost.
  - You are about to drop the column `archived` on the `user_finance_profile` table. All the data in the column will be lost.
  - You are about to drop the column `fixedSalary` on the `user_finance_profile` table. All the data in the column will be lost.
  - You are about to drop the column `name` on the `user_finance_profile` table. All the data in the column will be lost.
  - You are about to drop the column `debtPayed` on the `user_finance_report` table. All the data in the column will be lost.
  - You are about to drop the column `userId` on the `user_finance_report` table. All the data in the column will be lost.
  - You are about to drop the column `userFinanceReportId` on the `user_provider_revenue` table. All the data in the column will be lost.
  - You are about to drop the column `userFinanceReportId` on the `user_rule_profit_report` table. All the data in the column will be lost.
  - You are about to drop the column `userRuleProfitReportId` on the `user_rule_profit_report_entity` table. All the data in the column will be lost.
  - You are about to drop the `team_finance_report` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `user_account_spend_report` table. If the table is not empty, all the data it contains will be lost.
  - A unique constraint covering the columns `[userId]` on the table `user_finance_profile` will be added. If there are existing duplicate values, this will fail.
  - Added the required column `financeProfileId` to the `tiered_payout_rule` table without a default value. This is not possible if the table is not empty.
  - Added the required column `financeProfileId` to the `tiered_payout_rule_target` table without a default value. This is not possible if the table is not empty.
  - Added the required column `hourlyRate` to the `user_finance_profile` table without a default value. This is not possible if the table is not empty.
  - Added the required column `userFinanceProfileType` to the `user_finance_profile` table without a default value. This is not possible if the table is not empty.
  - Added the required column `financeProfileId` to the `user_finance_report` table without a default value. This is not possible if the table is not empty.
  - Added the required column `financeReportId` to the `user_provider_revenue` table without a default value. This is not possible if the table is not empty.
  - Added the required column `financeReportId` to the `user_rule_profit_report` table without a default value. This is not possible if the table is not empty.
  - Added the required column `ruleProfitReportId` to the `user_rule_profit_report_entity` table without a default value. This is not possible if the table is not empty.

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

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

-- CreateEnum
CREATE TYPE "Currency" AS ENUM ('USD', 'USDT', 'UAH', 'EUR');

-- DropForeignKey
ALTER TABLE "team_finance_report" DROP CONSTRAINT "team_finance_report_teamId_fkey";

-- DropForeignKey
ALTER TABLE "team_finance_report" DROP CONSTRAINT "team_finance_report_totalFinanceReportId_fkey";

-- DropForeignKey
ALTER TABLE "tiered_payout_rule" DROP CONSTRAINT "tiered_payout_rule_userFinanceProfileId_fkey";

-- DropForeignKey
ALTER TABLE "tiered_payout_rule_target" DROP CONSTRAINT "tiered_payout_rule_target_teamId_fkey";

-- DropForeignKey
ALTER TABLE "tiered_payout_rule_target" DROP CONSTRAINT "tiered_payout_rule_target_userId_fkey";

-- DropForeignKey
ALTER TABLE "user_account_spend_report" DROP CONSTRAINT "user_account_spend_report_accountId_fkey";

-- DropForeignKey
ALTER TABLE "user_account_spend_report" DROP CONSTRAINT "user_account_spend_report_userFinanceReportId_fkey";

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

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

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

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

-- AlterTable
ALTER TABLE "tiered_payout_rule" DROP COLUMN "userFinanceProfileId",
ADD COLUMN     "financeProfileId" INTEGER NOT NULL,
ADD COLUMN     "isReferral" BOOLEAN NOT NULL DEFAULT false;

-- AlterTable
ALTER TABLE "tiered_payout_rule_data" DROP COLUMN "thereshhold",
ADD COLUMN     "threshold" DOUBLE PRECISION NOT NULL DEFAULT 0;

-- AlterTable
ALTER TABLE "tiered_payout_rule_target" DROP COLUMN "teamId",
DROP COLUMN "userId",
ADD COLUMN     "financeProfileId" INTEGER NOT NULL;

-- AlterTable
ALTER TABLE "user_finance_profile" DROP COLUMN "active",
DROP COLUMN "archived",
DROP COLUMN "fixedSalary",
DROP COLUMN "name",
ADD COLUMN     "hourlyRate" DECIMAL(65,30) NOT NULL,
ADD COLUMN     "monthlyBaseSalary" DECIMAL(65,30) NOT NULL DEFAULT 0,
ADD COLUMN     "userFinanceProfileType" "UserFinanceProfileType" NOT NULL,
ALTER COLUMN "userId" DROP NOT NULL;

-- AlterTable
ALTER TABLE "user_finance_report" DROP COLUMN "debtPayed",
DROP COLUMN "userId",
ADD COLUMN     "financeProfileId" INTEGER NOT NULL;

-- AlterTable
ALTER TABLE "user_provider_revenue" DROP COLUMN "userFinanceReportId",
ADD COLUMN     "financeReportId" INTEGER NOT NULL;

-- AlterTable
ALTER TABLE "user_rule_profit_report" DROP COLUMN "userFinanceReportId",
ADD COLUMN     "financeReportId" INTEGER NOT NULL;

-- AlterTable
ALTER TABLE "user_rule_profit_report_entity" DROP COLUMN "userRuleProfitReportId",
ADD COLUMN     "ruleProfitReportId" INTEGER NOT NULL;

-- DropTable
DROP TABLE "team_finance_report";

-- DropTable
DROP TABLE "user_account_spend_report";

-- CreateTable
CREATE TABLE "account_spend_report" (
    "id" SERIAL NOT NULL,
    "spend" DOUBLE PRECISION NOT NULL,
    "fee" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "feePercent" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "feeSubscription" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "totalSpend" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "accountId" INTEGER NOT NULL,
    "userFinanceReportId" INTEGER NOT NULL,

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

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

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

-- CreateTable
CREATE TABLE "team_balance_default_setting" (
    "id" SERIAL NOT NULL,
    "defaultCurrentToOutstandingPayoutDay" INTEGER NOT NULL,
    "defaultCurrentToOutstandingPayoutAmount" DECIMAL(65,30) NOT NULL,
    "defaultCurrentToOutstandingPayoutPercent" DECIMAL(65,30) NOT NULL,
    "financeProfileType" "UserFinanceProfileType" NOT NULL,
    "teamId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "team_payout_default_setting" (
    "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 "team_payout_default_setting_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "team_finance_profile" (
    "id" SERIAL NOT NULL,
    "createDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateDate" TIMESTAMP(3) NOT NULL,
    "userFinanceProfileType" "UserFinanceProfileType" NOT NULL,
    "monthlyBaseSalary" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "hourlyRate" DECIMAL(65,30) NOT NULL,
    "teamId" INTEGER,

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

-- CreateTable
CREATE TABLE "finance_profile" (
    "id" SERIAL NOT NULL,
    "createDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateDate" TIMESTAMP(3) NOT NULL,
    "financeProfileEntityType" "FinanceProfileEntityType" NOT NULL,
    "payoutCurrency" "Currency" NOT NULL,
    "currentBalance" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "outstandingBalance" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "paidBalance" DECIMAL(65,30) NOT NULL DEFAULT 0,
    "teamFinanceProfileId" INTEGER,
    "userFinanceProfileId" INTEGER,

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

-- CreateTable
CREATE TABLE "transaction" (
    "id" SERIAL NOT NULL,
    "createDate" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updateDate" TIMESTAMP(3) NOT NULL,
    "amount" DECIMAL(65,30) NOT NULL,
    "currency" "Currency" NOT NULL,
    "title" TEXT NOT NULL,
    "financeProfileFromId" INTEGER NOT NULL,
    "financeProfileToId" INTEGER NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "team_balance_default_setting_teamId_defaultCurrentToOutstan_key" ON "team_balance_default_setting"("teamId", "defaultCurrentToOutstandingPayoutDay", "financeProfileType");

-- CreateIndex
CREATE UNIQUE INDEX "team_payout_default_setting_financeProfileId_currentToOutst_key" ON "team_payout_default_setting"("financeProfileId", "currentToOutstandingPayoutDay");

-- CreateIndex
CREATE UNIQUE INDEX "team_finance_profile_teamId_key" ON "team_finance_profile"("teamId");

-- CreateIndex
CREATE UNIQUE INDEX "finance_profile_teamFinanceProfileId_key" ON "finance_profile"("teamFinanceProfileId");

-- CreateIndex
CREATE UNIQUE INDEX "finance_profile_userFinanceProfileId_key" ON "finance_profile"("userFinanceProfileId");

-- CreateIndex
CREATE UNIQUE INDEX "user_finance_profile_userId_key" ON "user_finance_profile"("userId");

-- AddForeignKey
ALTER TABLE "account_spend_report" ADD CONSTRAINT "account_spend_report_accountId_fkey" FOREIGN KEY ("accountId") REFERENCES "account"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

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

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

-- AddForeignKey
ALTER TABLE "user_finance_report_additional_expense" ADD CONSTRAINT "user_finance_report_additional_expense_financeReportId_fkey" FOREIGN KEY ("financeReportId") REFERENCES "user_finance_report"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "user_provider_revenue" ADD CONSTRAINT "user_provider_revenue_financeReportId_fkey" FOREIGN KEY ("financeReportId") REFERENCES "user_finance_report"("id") ON DELETE CASCADE ON UPDATE CASCADE;

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

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

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

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

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

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

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

-- AddForeignKey
ALTER TABLE "finance_profile" ADD CONSTRAINT "finance_profile_teamFinanceProfileId_fkey" FOREIGN KEY ("teamFinanceProfileId") REFERENCES "team_finance_profile"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "finance_profile" ADD CONSTRAINT "finance_profile_userFinanceProfileId_fkey" FOREIGN KEY ("userFinanceProfileId") REFERENCES "user_finance_profile"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "transaction" ADD CONSTRAINT "transaction_financeProfileFromId_fkey" FOREIGN KEY ("financeProfileFromId") REFERENCES "finance_profile"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "transaction" ADD CONSTRAINT "transaction_financeProfileToId_fkey" FOREIGN KEY ("financeProfileToId") REFERENCES "finance_profile"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
