-- CreateTable
CREATE TABLE "automation_sessions" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userId" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'ACTIVE',
    "mode" TEXT NOT NULL DEFAULT 'SIMULATION',
    "symbol" TEXT NOT NULL,
    "timeframe" TEXT NOT NULL,
    "totalTrades" INTEGER NOT NULL DEFAULT 0,
    "successfulTrades" INTEGER NOT NULL DEFAULT 0,
    "failedTrades" INTEGER NOT NULL DEFAULT 0,
    "totalPnL" REAL NOT NULL DEFAULT 0,
    "totalPnLPercent" REAL NOT NULL DEFAULT 0,
    "winRate" REAL NOT NULL DEFAULT 0,
    "avgRiskReward" REAL NOT NULL DEFAULT 0,
    "maxDrawdown" REAL NOT NULL DEFAULT 0,
    "startBalance" REAL,
    "currentBalance" REAL,
    "settings" JSONB,
    "lastAnalysis" DATETIME,
    "lastTrade" DATETIME,
    "nextScheduled" DATETIME,
    "errorCount" INTEGER NOT NULL DEFAULT 0,
    "lastError" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "automation_sessions_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "ai_learning_data" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userId" TEXT NOT NULL,
    "sessionId" TEXT,
    "tradeId" TEXT,
    "analysisData" JSONB NOT NULL,
    "marketConditions" JSONB NOT NULL,
    "outcome" TEXT,
    "actualPrice" REAL,
    "predictedPrice" REAL,
    "confidenceScore" REAL,
    "accuracyScore" REAL,
    "timeframe" TEXT NOT NULL,
    "symbol" TEXT NOT NULL,
    "screenshot" TEXT,
    "feedbackData" JSONB,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "ai_learning_data_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_trades" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userId" TEXT NOT NULL,
    "symbol" TEXT NOT NULL,
    "side" TEXT NOT NULL,
    "amount" REAL NOT NULL,
    "price" REAL NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'PENDING',
    "driftTxId" TEXT,
    "profit" REAL,
    "fees" REAL,
    "screenshotUrl" TEXT,
    "aiAnalysis" TEXT,
    "isAutomated" BOOLEAN NOT NULL DEFAULT false,
    "entryPrice" REAL,
    "exitPrice" REAL,
    "stopLoss" REAL,
    "takeProfit" REAL,
    "leverage" REAL,
    "timeframe" TEXT,
    "tradingMode" TEXT,
    "confidence" REAL,
    "marketSentiment" TEXT,
    "outcome" TEXT,
    "pnlPercent" REAL,
    "actualRR" REAL,
    "executionTime" DATETIME,
    "learningData" JSONB,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    "executedAt" DATETIME,
    "closedAt" DATETIME,
    CONSTRAINT "trades_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "new_trades" ("aiAnalysis", "amount", "closedAt", "createdAt", "driftTxId", "executedAt", "fees", "id", "price", "profit", "screenshotUrl", "side", "status", "symbol", "updatedAt", "userId") SELECT "aiAnalysis", "amount", "closedAt", "createdAt", "driftTxId", "executedAt", "fees", "id", "price", "profit", "screenshotUrl", "side", "status", "symbol", "updatedAt", "userId" FROM "trades";
DROP TABLE "trades";
ALTER TABLE "new_trades" RENAME TO "trades";
CREATE TABLE "new_trading_journals" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userId" TEXT NOT NULL,
    "date" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "screenshotUrl" TEXT NOT NULL,
    "aiAnalysis" TEXT NOT NULL,
    "marketSentiment" TEXT,
    "keyLevels" JSONB,
    "recommendation" TEXT NOT NULL,
    "confidence" REAL NOT NULL,
    "notes" TEXT,
    "isAutomated" BOOLEAN NOT NULL DEFAULT false,
    "symbol" TEXT,
    "timeframe" TEXT,
    "tradingMode" TEXT,
    "tradeId" TEXT,
    "outcome" TEXT,
    "actualPrice" REAL,
    "priceAtAnalysis" REAL,
    "accuracyScore" REAL,
    "executionDelay" INTEGER,
    "marketCondition" TEXT,
    "sessionId" TEXT,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "trading_journals_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "new_trading_journals" ("aiAnalysis", "confidence", "createdAt", "date", "id", "keyLevels", "marketSentiment", "notes", "recommendation", "screenshotUrl", "updatedAt", "userId") SELECT "aiAnalysis", "confidence", "createdAt", "date", "id", "keyLevels", "marketSentiment", "notes", "recommendation", "screenshotUrl", "updatedAt", "userId" FROM "trading_journals";
DROP TABLE "trading_journals";
ALTER TABLE "new_trading_journals" RENAME TO "trading_journals";
CREATE TABLE "new_user_settings" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "userId" TEXT NOT NULL,
    "autoTrading" BOOLEAN NOT NULL DEFAULT false,
    "tradingAmount" REAL NOT NULL DEFAULT 100,
    "riskPercentage" REAL NOT NULL DEFAULT 2,
    "maxDailyTrades" INTEGER NOT NULL DEFAULT 5,
    "enableNotifications" BOOLEAN NOT NULL DEFAULT true,
    "automationMode" TEXT NOT NULL DEFAULT 'SIMULATION',
    "autoTimeframe" TEXT NOT NULL DEFAULT '1h',
    "autoSymbol" TEXT NOT NULL DEFAULT 'SOLUSD',
    "autoTradingEnabled" BOOLEAN NOT NULL DEFAULT false,
    "autoAnalysisEnabled" BOOLEAN NOT NULL DEFAULT false,
    "maxLeverage" REAL NOT NULL DEFAULT 3.0,
    "stopLossPercent" REAL NOT NULL DEFAULT 2.0,
    "takeProfitPercent" REAL NOT NULL DEFAULT 6.0,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "user_settings_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "new_user_settings" ("autoTrading", "createdAt", "enableNotifications", "id", "maxDailyTrades", "riskPercentage", "tradingAmount", "updatedAt", "userId") SELECT "autoTrading", "createdAt", "enableNotifications", "id", "maxDailyTrades", "riskPercentage", "tradingAmount", "updatedAt", "userId" FROM "user_settings";
DROP TABLE "user_settings";
ALTER TABLE "new_user_settings" RENAME TO "user_settings";
CREATE UNIQUE INDEX "user_settings_userId_key" ON "user_settings"("userId");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

-- CreateIndex
CREATE UNIQUE INDEX "automation_sessions_userId_symbol_timeframe_key" ON "automation_sessions"("userId", "symbol", "timeframe");
