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

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

model User {
  id             Int              @id @default(autoincrement())
  username       String           @unique
  passwordHash   String
  balance        String           @default("0")
  osuBalance     Int              @default(0)
  prBalance      String           @default("0")
  lastAdAt       DateTime?
  role           String           @default("user")
  avatarUrl      String           @default("")
  bio            String           @default("")
  autoSellEnabled Boolean         @default(false)
  autoSellThresholdCents String   @default("1000")
  prestigeCount            Int    @default(0)
  prestigeChanceBonus      Int    @default(0)
  prestigeGainBonus        Int    @default(0)
  prestigeWearBonus        Int    @default(0)
  prestigePrBonus          Int    @default(0)
  prestigeOpenBonus        Int    @default(0)
  prestigeQualityBonus     Int    @default(0)
  prestigeAnimReduction    Int    @default(0)
  prestigeRefundChance     Int    @default(0)
  prestigeRespinChance     Int    @default(0)
  prestigeVaultSlots       Int    @default(0)
  prestigeOfflineYield     Int    @default(0)
  prestigeOnlineYield      Int    @default(0)
  vaultYieldAt     DateTime?
  lastConnection DateTime?
  playTimeSeconds BigInt          @default(0)
  createdAt      DateTime         @default(now())
  inventory    InventoryItem[]
  transactions Transaction[]
  jackpotWins    JackpotRound[]   @relation("JackpotWinner")
  jackpotCreated JackpotRound[]   @relation("JackpotCreator")
  jackpotDeposits JackpotDeposit[]
  caseKeyProgress CaseKeyProgress[]
  catalogEntries CatalogEntry[]
  prestigeLogs   PrestigeLog[]
  achievements   UserAchievement[]
  paymentOrders  PaymentOrder[]
}

model PaymentOrder {
  id          String    @id @default(cuid())
  userId      Int
  packId      String
  osuAmount   Int
  priceCents  Int
  currency    String    @default("eur")
  status      String    @default("pending")
  provider    String    @default("mock")
  providerRef String    @default("")
  meta        String    @default("{}")
  createdAt   DateTime  @default(now())
  paidAt      DateTime?
  user        User      @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@index([userId, status])
  @@index([providerRef])
}

model Case {
  id        Int        @id @default(autoincrement())
  name      String
  imageUrl  String     @default("")
  price     String     @default("0")
  active    Boolean    @default(true)
  createdAt DateTime   @default(now())
  items     CaseItem[]
  keyProgress CaseKeyProgress[]
  catalogEntries CatalogEntry[]
}

model Category {
  id        Int      @id @default(autoincrement())
  name      String   @unique
  createdAt DateTime @default(now())
}

model Item {
  id          Int             @id @default(autoincrement())
  name        String
  imageUrl    String          @default("")
  rarity      String
  category    String          @default("Misc")
  marketValue String          @default("0")
  createdAt   DateTime        @default(now())
  cases       CaseItem[]
  inventory   InventoryItem[]
  catalogEntries CatalogEntry[]
}

model CaseItem {
  id       Int   @id @default(autoincrement())
  caseId   Int
  itemId   Int
  dropRate Float
  case     Case  @relation(fields: [caseId], references: [id], onDelete: Cascade)
  item     Item  @relation(fields: [itemId], references: [id], onDelete: Cascade)

  @@unique([caseId, itemId])
}

model InventoryItem {
  id          Int      @id @default(autoincrement())
  userId      Int
  itemId      Int
  locked      Boolean  @default(false)
  staked      Boolean  @default(false)
  stakedAt    DateTime?
  lastYieldAt DateTime?
  floatValue  Float    @default(0.265)
  wear        String   @default("Field-Tested")
  paintSeed   Int      @default(0)
  valueCents  String   @default("0")
  favorite    Boolean  @default(false)
  obtainedAt  DateTime @default(now())
  user        User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  item        Item     @relation(fields: [itemId], references: [id], onDelete: Cascade)
  jackpotDeposits JackpotDeposit[]
}

model Transaction {
  id        Int      @id @default(autoincrement())
  userId    Int
  type      String
  amount    String
  meta      String   @default("{}")
  createdAt DateTime @default(now())
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model JackpotRound {
  id           Int              @id @default(autoincrement())
  status       String           @default("open")
  endsAt       DateTime
  creatorId    Int?
  winnerUserId Int?
  createdAt    DateTime         @default(now())
  resolvedAt   DateTime?
  creator      User?            @relation("JackpotCreator", fields: [creatorId], references: [id])
  winner       User?            @relation("JackpotWinner", fields: [winnerUserId], references: [id])
  deposits     JackpotDeposit[]

  @@index([status])
}

model JackpotDeposit {
  id              Int           @id @default(autoincrement())
  roundId         Int
  userId          Int
  inventoryItemId Int
  valueCents      String
  createdAt       DateTime      @default(now())
  round           JackpotRound  @relation(fields: [roundId], references: [id], onDelete: Cascade)
  user            User          @relation(fields: [userId], references: [id], onDelete: Cascade)
  inventoryItem   InventoryItem @relation(fields: [inventoryItemId], references: [id], onDelete: Cascade)

  @@unique([roundId, inventoryItemId])
}

model CaseKeyProgress {
  id                Int  @id @default(autoincrement())
  userId            Int
  caseId            Int
  keys              Int  @default(1)
  opensSinceLastKey Int  @default(0)
  totalOpens        Int  @default(0)
  user              User @relation(fields: [userId], references: [id], onDelete: Cascade)
  case              Case @relation(fields: [caseId], references: [id], onDelete: Cascade)

  @@unique([userId, caseId])
}

model CatalogEntry {
  id       Int      @id @default(autoincrement())
  userId   Int
  caseId   Int
  itemId   Int
  filledAt DateTime @default(now())
  user     User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  case     Case     @relation(fields: [caseId], references: [id], onDelete: Cascade)
  item     Item     @relation(fields: [itemId], references: [id], onDelete: Cascade)

  @@unique([userId, caseId, itemId])
}

model PrestigeLog {
  id        Int      @id @default(autoincrement())
  userId    Int
  choice    String
  prGranted String   @default("0")
  createdAt DateTime @default(now())
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model Achievement {
  id          Int               @id @default(autoincrement())
  key         String            @unique
  name        String
  description String
  icon        String            @default("")
  sortOrder   Int               @default(0)
  createdAt   DateTime          @default(now())
  unlocks     UserAchievement[]
}

model UserAchievement {
  id            Int         @id @default(autoincrement())
  userId        Int
  achievementId Int
  unlockedAt    DateTime    @default(now())
  user          User        @relation(fields: [userId], references: [id], onDelete: Cascade)
  achievement   Achievement @relation(fields: [achievementId], references: [id], onDelete: Cascade)

  @@unique([userId, achievementId])
}

/** Key/value app settings (e.g. shop.osuTopupsEnabled). */
model AppSetting {
  key       String   @id
  value     String
  updatedAt DateTime @updatedAt
}

/** Configurable real-money → osu packs for the shop. */
model ShopOsuPack {
  id          String   @id
  name        String
  osu         Int
  priceCents  Int
  currency    String   @default("eur")
  badge       String   @default("")
  blurb       String   @default("")
  perOsuLabel String   @default("")
  enabled     Boolean  @default(true)
  sortOrder   Int      @default(0)
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt
}
