-- CreateEnum
CREATE TYPE "TradingAccountType" AS ENUM ('live', 'demo');

-- CreateEnum
CREATE TYPE "TradingAccountTransactionType" AS ENUM ('deposit', 'withdrawal');

-- CreateEnum
CREATE TYPE "TradingMarket" AS ENUM ('forex', 'stocks', 'commodities', 'cryptocurrency', 'indices', 'futures', 'options');

-- CreateEnum
CREATE TYPE "TradeExecutionAction" AS ENUM ('buy', 'sell');

-- CreateTable
CREATE TABLE "trading_accounts" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "broker" TEXT,
    "type" "TradingAccountType" NOT NULL,
    "init_balance" JSONB NOT NULL,
    "default_currency" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "trading_account_transactions" (
    "id" TEXT NOT NULL,
    "trading_account_id" TEXT NOT NULL,
    "type" "TradingAccountTransactionType" NOT NULL,
    "amount" DOUBLE PRECISION NOT NULL,
    "currency" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "trading_systems" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "description" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "trades" (
    "id" TEXT NOT NULL,
    "user_id" TEXT NOT NULL,
    "trading_account_id" TEXT NOT NULL,
    "trading_system_id" TEXT NOT NULL,
    "market" "TradingMarket" NOT NULL,
    "currency" TEXT,
    "instrument" TEXT NOT NULL,
    "timeframe" INTEGER NOT NULL,
    "take_profit" DOUBLE PRECISION,
    "stop_loss" DOUBLE PRECISION,
    "highest_price" DOUBLE PRECISION,
    "lowest_price" DOUBLE PRECISION,
    "screenshots" JSONB,
    "comment" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "trade_executions" (
    "id" TEXT NOT NULL,
    "trade_id" TEXT NOT NULL,
    "action" "TradeExecutionAction" NOT NULL,
    "datetime" TIMESTAMP(3) NOT NULL,
    "shares" INTEGER NOT NULL,
    "price" DOUBLE PRECISION NOT NULL,
    "pl" DOUBLE PRECISION NOT NULL,
    "fees" DOUBLE PRECISION,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- AddForeignKey
ALTER TABLE "trading_accounts" ADD CONSTRAINT "trading_accounts_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "trading_account_transactions" ADD CONSTRAINT "trading_account_transactions_trading_account_id_fkey" FOREIGN KEY ("trading_account_id") REFERENCES "trading_accounts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "trading_systems" ADD CONSTRAINT "trading_systems_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "trades" ADD CONSTRAINT "trades_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "trades" ADD CONSTRAINT "trades_trading_account_id_fkey" FOREIGN KEY ("trading_account_id") REFERENCES "trading_accounts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "trades" ADD CONSTRAINT "trades_trading_system_id_fkey" FOREIGN KEY ("trading_system_id") REFERENCES "trading_systems"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "trade_executions" ADD CONSTRAINT "trade_executions_trade_id_fkey" FOREIGN KEY ("trade_id") REFERENCES "trades"("id") ON DELETE CASCADE ON UPDATE CASCADE;
