generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

model ai_learning_data {
  id               String   @id @default(cuid())
  userId           String
  sessionId        String?
  tradeId          String?
  analysisData     Json
  marketConditions Json
  outcome          String?
  actualPrice      Float?
  predictedPrice   Float?
  confidenceScore  Float?
  accuracyScore    Float?
  timeframe        String
  symbol           String
  screenshot       String?
  feedbackData     Json?
  createdAt        DateTime @default(now())
  updatedAt        DateTime @updatedAt
  users            users    @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model api_keys {
  id           String   @id
  userId       String
  provider     String
  keyName      String
  encryptedKey String
  isActive     Boolean  @default(true)
  createdAt    DateTime @default(now())
  updatedAt    DateTime @updatedAt
  users        users    @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([userId, provider, keyName])
}

model automation_sessions {
  id               String    @id
  userId           String
  status           String    @default("ACTIVE")
  mode             String    @default("SIMULATION")
  symbol           String
  timeframe        String
  totalTrades      Int       @default(0)
  successfulTrades Int       @default(0)
  failedTrades     Int       @default(0)
  totalPnL         Float     @default(0)
  totalPnLPercent  Float     @default(0)
  winRate          Float     @default(0)
  avgRiskReward    Float     @default(0)
  maxDrawdown      Float     @default(0)
  startBalance     Float?
  currentBalance   Float?
  settings         Json?
  lastAnalysis     DateTime?
  lastTrade        DateTime?
  nextScheduled    DateTime?
  errorCount       Int       @default(0)
  lastError        String?
  createdAt        DateTime  @default(now())
  updatedAt        DateTime  @updatedAt
  lastAnalysisData Json?
  users            users     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([userId, symbol, timeframe])
}

model screenshots {
  id        String   @id
  url       String
  filename  String
  fileSize  Int
  mimeType  String
  metadata  Json?
  createdAt DateTime @default(now())
}

model system_logs {
  id        String   @id
  level     String
  message   String
  metadata  Json?
  createdAt DateTime @default(now())
}

model trades {
  id              String    @id
  userId          String
  symbol          String
  side            String
  amount          Float
  price           Float
  status          String    @default("PENDING")
  driftTxId       String?
  profit          Float?
  fees            Float?
  screenshotUrl   String?
  aiAnalysis      String?
  isAutomated     Boolean   @default(false)
  entryPrice      Float?
  exitPrice       Float?
  stopLoss        Float?
  takeProfit      Float?
  leverage        Float?
  timeframe       String?
  tradingMode     String?
  confidence      Float?
  marketSentiment String?
  outcome         String?
  pnlPercent      Float?
  actualRR        Float?
  executionTime   DateTime?
  learningData    Json?
  createdAt       DateTime  @default(now())
  updatedAt       DateTime  @updatedAt
  executedAt      DateTime?
  closedAt        DateTime?
  users           users     @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model trading_journals {
  id              String   @id
  userId          String
  date            DateTime @default(now())
  screenshotUrl   String
  aiAnalysis      String
  marketSentiment String?
  keyLevels       Json?
  recommendation  String
  confidence      Float
  notes           String?
  isAutomated     Boolean  @default(false)
  symbol          String?
  timeframe       String?
  tradingMode     String?
  tradeId         String?
  outcome         String?
  actualPrice     Float?
  priceAtAnalysis Float?
  accuracyScore   Float?
  executionDelay  Int?
  marketCondition String?
  sessionId       String?
  createdAt       DateTime @default(now())
  updatedAt       DateTime @updatedAt
  users           users    @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model user_settings {
  id                  String   @id
  userId              String   @unique
  autoTrading         Boolean  @default(false)
  tradingAmount       Float    @default(100)
  riskPercentage      Float    @default(2)
  maxDailyTrades      Int      @default(5)
  enableNotifications Boolean  @default(true)
  automationMode      String   @default("SIMULATION")
  autoTimeframe       String   @default("1h")
  autoSymbol          String   @default("SOLUSD")
  autoTradingEnabled  Boolean  @default(false)
  autoAnalysisEnabled Boolean  @default(false)
  maxLeverage         Float    @default(3.0)
  stopLossPercent     Float    @default(2.0)
  takeProfitPercent   Float    @default(6.0)
  createdAt           DateTime @default(now())
  updatedAt           DateTime @updatedAt
  users               users    @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model users {
  id                  String                @id
  email               String                @unique
  name                String?
  createdAt           DateTime              @default(now())
  updatedAt           DateTime              @updatedAt
  ai_learning_data    ai_learning_data[]
  api_keys            api_keys[]
  automation_sessions automation_sessions[]
  trades              trades[]
  trading_journals    trading_journals[]
  user_settings       user_settings?
}
