generator client {
  provider        = "prisma-client-js"
  previewFeatures = []
  binaryTargets   = ["debian-openssl-3.0.x", "linux-arm64-openssl-3.0.x", "native"]
}

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

model Access {
  alias         String?
  createdAt     DateTime           @default(now())
  granteeUserId String?
  id            String             @id @default(uuid())
  permissions   AccessPermission[] @default([READ_RESTRICTED])
  updatedAt     DateTime           @updatedAt
  userId        String
  GranteeUser   User?              @relation("accessGet", fields: [granteeUserId], onDelete: Cascade, references: [id])
  User          User               @relation("accessGive", fields: [userId], onDelete: Cascade, references: [id])

  @@index([alias])
  @@index([granteeUserId])
  @@index([userId])
}

model Account {
  balance    Float            @default(0)
  balances   AccountBalance[]
  comment    String?
  createdAt  DateTime         @default(now())
  currency   String?
  id         String           @default(uuid())
  isExcluded Boolean          @default(false)
  name       String?
  platformId String?
  updatedAt  DateTime         @updatedAt
  userId     String
  Platform   Platform?        @relation(fields: [platformId], references: [id])
  User       User             @relation(fields: [userId], onDelete: Cascade, references: [id])
  Order      Order[]

  @@id([id, userId])
  @@index([currency])
  @@index([id])
  @@index([isExcluded])
  @@index([name])
  @@index([userId])
}

model AccountBalance {
  accountId String
  createdAt DateTime @default(now())
  date      DateTime @default(now())
  id        String   @id @default(uuid())
  updatedAt DateTime @updatedAt
  userId    String
  value     Float
  Account   Account  @relation(fields: [accountId, userId], onDelete: Cascade, references: [id, userId])

  @@unique([accountId, date])
  @@index([accountId])
  @@index([date])
}

model Analytics {
  activityCount                       Int      @default(0)
  country                             String?
  dataProviderGhostfolioDailyRequests Int      @default(0)
  lastRequestAt                       DateTime @default(now())
  updatedAt                           DateTime @updatedAt
  userId                              String   @id
  User                                User     @relation(fields: [userId], onDelete: Cascade, references: [id])

  @@index([lastRequestAt])
  @@index([updatedAt])
}

model ApiKey {
  createdAt DateTime @default(now())
  hashedKey String   @unique
  id        String   @id @default(uuid())
  updatedAt DateTime @updatedAt
  userId    String
  user      User     @relation(fields: [userId], onDelete: Cascade, references: [id])

  @@index([userId])
}

model AuthDevice {
  createdAt           DateTime @default(now())
  credentialId        Bytes
  credentialPublicKey Bytes
  counter             Int
  id                  String   @id @default(uuid())
  updatedAt           DateTime @updatedAt
  userId              String
  User                User     @relation(fields: [userId], onDelete: Cascade, references: [id])

  @@index([userId])
}

model MarketData {
  createdAt   DateTime        @default(now())
  dataSource  DataSource
  date        DateTime
  id          String          @id @default(uuid())
  marketPrice Float
  state       MarketDataState @default(CLOSE)
  symbol      String

  @@unique([dataSource, date, symbol])
  @@index([dataSource])
  @@index([dataSource, symbol])
  @@index([date])
  @@index([marketPrice])
  @@index([state])
  @@index([symbol])
}

model Order {
  accountId       String?
  accountUserId   String?
  comment         String?
  createdAt       DateTime      @default(now())
  currency        String?
  date            DateTime
  fee             Float
  id              String        @id @default(uuid())
  isDraft         Boolean       @default(false)
  quantity        Float
  symbolProfileId String
  type            Type
  unitPrice       Float
  updatedAt       DateTime      @updatedAt
  userId          String
  Account         Account?      @relation(fields: [accountId, accountUserId], references: [id, userId])
  SymbolProfile   SymbolProfile @relation(fields: [symbolProfileId], references: [id])
  User            User          @relation(fields: [userId], onDelete: Cascade, references: [id])
  tags            Tag[]

  @@index([accountId])
  @@index([date])
  @@index([isDraft])
  @@index([userId])
}

model Platform {
  id      String    @id @default(uuid())
  name    String?
  url     String    @unique
  Account Account[]

  @@index([name])
}

model Property {
  key   String @id
  value String
}

model Settings {
  settings  Json?
  updatedAt DateTime @updatedAt
  userId    String   @id
  User      User     @relation(fields: [userId], onDelete: Cascade, references: [id])
}

model SymbolProfile {
  assetClass             AssetClass?
  assetSubClass          AssetSubClass?
  comment                String?
  countries              Json?
  createdAt              DateTime                @default(now())
  currency               String
  dataSource             DataSource
  figi                   String?
  figiComposite          String?
  figiShareClass         String?
  holdings               Json?                   @default("[]")
  id                     String                  @id @default(uuid())
  isin                   String?
  name                   String?
  updatedAt              DateTime                @updatedAt
  scraperConfiguration   Json?
  sectors                Json?
  symbol                 String
  symbolMapping          Json?
  url                    String?
  userId                 String?
  Order                  Order[]
  SymbolProfileOverrides SymbolProfileOverrides?
  User                   User?                   @relation(fields: [userId], onDelete: Cascade, references: [id])

  @@unique([dataSource, symbol])
  @@index([assetClass])
  @@index([currency])
  @@index([dataSource])
  @@index([isin])
  @@index([name])
  @@index([symbol])
}

model SymbolProfileOverrides {
  assetClass      AssetClass?
  assetSubClass   AssetSubClass?
  countries       Json?          @default("[]")
  holdings        Json?          @default("[]")
  name            String?
  sectors         Json?          @default("[]")
  symbolProfileId String         @id
  updatedAt       DateTime       @updatedAt
  url             String?
  SymbolProfile   SymbolProfile  @relation(fields: [symbolProfileId], onDelete: Cascade, references: [id])
}

model Subscription {
  createdAt DateTime @default(now())
  expiresAt DateTime
  id        String   @id @default(uuid())
  price     Float?
  updatedAt DateTime @updatedAt
  userId    String
  User      User     @relation(fields: [userId], onDelete: Cascade, references: [id])

  @@index([userId])
}

model Tag {
  id     String  @id @default(uuid())
  name   String
  orders Order[]
  userId String?
  User   User?   @relation(fields: [userId], onDelete: Cascade, references: [id])

  @@unique([name, userId])
  @@index([name])
}

model User {
  accessToken   String?
  authChallenge String?
  createdAt     DateTime        @default(now())
  id            String          @id @default(uuid())
  provider      Provider        @default(ANONYMOUS)
  role          Role            @default(USER)
  thirdPartyId  String?
  updatedAt     DateTime        @updatedAt
  Access        Access[]        @relation("accessGet")
  AccessGive    Access[]        @relation("accessGive")
  Account       Account[]
  Analytics     Analytics?
  ApiKey        ApiKey[]
  AuthDevice    AuthDevice[]
  Order         Order[]
  Settings      Settings?
  Subscription  Subscription[]
  SymbolProfile SymbolProfile[]
  Tag           Tag[]

  @@index([accessToken])
  @@index([createdAt])
  @@index([provider])
  @@index([role])
  @@index([thirdPartyId])
}

enum AccessPermission {
  READ
  READ_RESTRICTED
}

enum AssetClass {
  COMMODITY
  EQUITY
  FIXED_INCOME
  LIQUIDITY
  REAL_ESTATE
}

enum AssetSubClass {
  BOND
  CASH
  COMMODITY
  CRYPTOCURRENCY
  ETF
  MUTUALFUND
  PRECIOUS_METAL
  PRIVATE_EQUITY
  STOCK
}

enum DataSource {
  ALPHA_VANTAGE
  COINGECKO
  EOD_HISTORICAL_DATA
  FINANCIAL_MODELING_PREP
  GHOSTFOLIO
  GOOGLE_SHEETS
  MANUAL
  RAPID_API
  YAHOO
}

enum MarketDataState {
  CLOSE
  INTRADAY
}

enum Provider {
  ANONYMOUS
  GOOGLE
  INTERNET_IDENTITY
}

enum Role {
  ADMIN
  DEMO
  INACTIVE
  USER
}

enum Type {
  BUY
  DIVIDEND
  FEE
  INTEREST
  ITEM
  LIABILITY
  SELL
}

enum ViewMode {
  DEFAULT
  ZEN
}
