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

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

model User {
  id        String           @id @default(cuid())
  email     String           @unique
  name      String?
  createdAt DateTime         @default(now())
  updatedAt DateTime         @updatedAt
  apiKeys   ApiKey[]
  trades    Trade[]
  journals  TradingJournal[]
  settings  UserSettings?

  @@map("users")
}

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

  @@unique([userId, provider, keyName])
  @@map("api_keys")
}

model UserSettings {
  id                  String   @id @default(cuid())
  userId              String   @unique
  autoTrading         Boolean  @default(false)
  tradingAmount       Float    @default(100)
  riskPercentage      Float    @default(2)
  maxDailyTrades      Int      @default(5)
  enableNotifications Boolean  @default(true)
  createdAt           DateTime @default(now())
  updatedAt           DateTime @updatedAt
  user                User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@map("user_settings")
}

model Trade {
  id            String    @id @default(cuid())
  userId        String
  symbol        String
  side          String
  amount        Float
  price         Float
  status        String    @default("PENDING")
  driftTxId     String?
  profit        Float?
  fees          Float?
  screenshotUrl String?
  aiAnalysis    String?
  createdAt     DateTime  @default(now())
  updatedAt     DateTime  @updatedAt
  executedAt    DateTime?
  closedAt      DateTime?
  user          User      @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@map("trades")
}

model TradingJournal {
  id              String   @id @default(cuid())
  userId          String
  date            DateTime @default(now())
  screenshotUrl   String
  aiAnalysis      String
  marketSentiment String?
  keyLevels       Json?
  recommendation  String
  confidence      Float
  notes           String?
  createdAt       DateTime @default(now())
  updatedAt       DateTime @updatedAt
  user            User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@map("trading_journals")
}

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

  @@map("screenshots")
}

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

  @@map("system_logs")
}
