/*
  Warnings:

  - The values [buy,sell] on the enum `TradeExecutionAction` will be removed. If these variants are still used in the database, this will fail.
  - The values [deposit,withdrawal] on the enum `TradingAccountTransactionType` will be removed. If these variants are still used in the database, this will fail.
  - The values [live,demo] on the enum `TradingAccountType` will be removed. If these variants are still used in the database, this will fail.
  - The values [forex,stocks,commodities,cryptocurrency,indices,futures,options] on the enum `TradingMarket` will be removed. If these variants are still used in the database, this will fail.
  - You are about to drop the column `fees` on the `trade_executions` table. All the data in the column will be lost.
  - You are about to drop the column `pl` on the `trade_executions` table. All the data in the column will be lost.
  - You are about to drop the column `price` on the `trade_executions` table. All the data in the column will be lost.
  - You are about to drop the column `shares` on the `trade_executions` table. All the data in the column will be lost.
  - You are about to drop the column `user_id` on the `trades` table. All the data in the column will be lost.
  - You are about to drop the column `init_balance` on the `trading_accounts` table. All the data in the column will be lost.
  - Added the required column `execution_date` to the `trade_executions` table without a default value. This is not possible if the table is not empty.
  - Added the required column `execution_price` to the `trade_executions` table without a default value. This is not possible if the table is not empty.
  - Added the required column `volume` to the `trade_executions` table without a default value. This is not possible if the table is not empty.
  - Made the column `currency` on table `trades` required. This step will fail if there are existing NULL values in that column.

*/
-- AlterEnum
BEGIN;
CREATE TYPE "TradeExecutionAction_new" AS ENUM ('BUY', 'SELL', 'CALL', 'PUT');
ALTER TABLE "trade_executions" ALTER COLUMN "action" TYPE "TradeExecutionAction_new" USING ("action"::text::"TradeExecutionAction_new");
ALTER TYPE "TradeExecutionAction" RENAME TO "TradeExecutionAction_old";
ALTER TYPE "TradeExecutionAction_new" RENAME TO "TradeExecutionAction";
DROP TYPE "public"."TradeExecutionAction_old";
COMMIT;

-- AlterEnum
BEGIN;
CREATE TYPE "TradingAccountTransactionType_new" AS ENUM ('DEPOSIT', 'WITHDRAW', 'WIN', 'LOSS');
ALTER TABLE "trading_account_transactions" ALTER COLUMN "type" TYPE "TradingAccountTransactionType_new" USING ("type"::text::"TradingAccountTransactionType_new");
ALTER TYPE "TradingAccountTransactionType" RENAME TO "TradingAccountTransactionType_old";
ALTER TYPE "TradingAccountTransactionType_new" RENAME TO "TradingAccountTransactionType";
DROP TYPE "public"."TradingAccountTransactionType_old";
COMMIT;

-- AlterEnum
BEGIN;
CREATE TYPE "TradingAccountType_new" AS ENUM ('LIVE', 'DEMO');
ALTER TABLE "trading_accounts" ALTER COLUMN "type" TYPE "TradingAccountType_new" USING ("type"::text::"TradingAccountType_new");
ALTER TYPE "TradingAccountType" RENAME TO "TradingAccountType_old";
ALTER TYPE "TradingAccountType_new" RENAME TO "TradingAccountType";
DROP TYPE "public"."TradingAccountType_old";
COMMIT;

-- AlterEnum
BEGIN;
CREATE TYPE "TradingMarket_new" AS ENUM ('FOREX', 'STOCKS', 'COMMODITIES', 'CRYPTOCURRENCY', 'INDICES', 'FUTURES', 'OPTIONS');
ALTER TABLE "trades" ALTER COLUMN "market" TYPE "TradingMarket_new" USING ("market"::text::"TradingMarket_new");
ALTER TYPE "TradingMarket" RENAME TO "TradingMarket_old";
ALTER TYPE "TradingMarket_new" RENAME TO "TradingMarket";
DROP TYPE "public"."TradingMarket_old";
COMMIT;

-- DropForeignKey
ALTER TABLE "trades" DROP CONSTRAINT "trades_user_id_fkey";

-- AlterTable
ALTER TABLE "trade_executions" DROP COLUMN "fees",
DROP COLUMN "pl",
DROP COLUMN "price",
DROP COLUMN "shares",
ADD COLUMN     "execution_date" TIMESTAMP(3) NOT NULL,
ADD COLUMN     "execution_price" DOUBLE PRECISION NOT NULL,
ADD COLUMN     "fee" DOUBLE PRECISION NOT NULL DEFAULT 0,
ADD COLUMN     "is_closing" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN     "profit_loss" DOUBLE PRECISION NOT NULL DEFAULT 0,
ADD COLUMN     "volume" DOUBLE PRECISION NOT NULL;

-- AlterTable
ALTER TABLE "trades" DROP COLUMN "user_id",
ADD COLUMN     "duration" INTEGER,
ALTER COLUMN "currency" SET NOT NULL;

-- AlterTable
ALTER TABLE "trading_accounts" DROP COLUMN "init_balance",
ALTER COLUMN "type" SET DEFAULT 'DEMO';

-- CreateTable
CREATE TABLE "trading_account_balance" (
    "tradingAccountId" TEXT NOT NULL,
    "currency" TEXT NOT NULL,
    "initial" DOUBLE PRECISION NOT NULL,
    "current" DOUBLE PRECISION NOT NULL,

    CONSTRAINT "trading_account_balance_pkey" PRIMARY KEY ("tradingAccountId","currency")
);

-- AddForeignKey
ALTER TABLE "trading_account_balance" ADD CONSTRAINT "trading_account_balance_tradingAccountId_fkey" FOREIGN KEY ("tradingAccountId") REFERENCES "trading_accounts"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
